Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[jra/samba/.git] / source4 / winbind / wb_cmd_getpwent.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for getpwent
5
6    Copyright (C) Kai Blin 2007
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "winbind/wb_async_helpers.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28 #include "libnet/libnet_proto.h"
29
30 struct cmd_getpwent_state {
31         struct composite_context *ctx;
32         struct wbsrv_service *service;
33
34         struct wbsrv_pwent *pwent;
35         uint32_t max_users;
36
37         uint32_t num_users;
38         struct winbindd_pw *result;
39 };
40
41 static void cmd_getpwent_recv_pwnam(struct composite_context *ctx);
42 #if 0 /*FIXME: implement this*/
43 static void cmd_getpwent_recv_user_list(struct composite_context *ctx);
44 #endif
45
46 struct composite_context *wb_cmd_getpwent_send(TALLOC_CTX *mem_ctx,
47                 struct wbsrv_service *service, struct wbsrv_pwent *pwent,
48                 uint32_t max_users)
49 {
50         struct composite_context *ctx, *result;
51         struct cmd_getpwent_state *state;
52
53         DEBUG(5, ("wb_cmd_getpwent_send called\n"));
54
55         result = composite_create(mem_ctx, service->task->event_ctx);
56         if (!result) return NULL;
57
58         state = talloc(mem_ctx, struct cmd_getpwent_state);
59         if (composite_nomem(state, result)) return result;
60
61         state->ctx = result;
62         result->private_data = state;
63         state->service = service;
64         state->pwent = pwent;
65         state->max_users = max_users;
66         state->num_users = 0;
67
68         /* If there are users left in the libnet_UserList and we're below the
69          * maximum number of users to get per winbind getpwent call, use
70          * getpwnam to get the winbindd_pw struct */
71         if (pwent->page_index < pwent->user_list->out.count) {
72                 int idx = pwent->page_index;
73                 char *username = talloc_strdup(state,
74                         pwent->user_list->out.users[idx].username);
75
76                 pwent->page_index++;
77                 ctx = wb_cmd_getpwnam_send(state, service, username);
78                 if (composite_nomem(ctx, state->ctx)) return result;
79
80                 composite_continue(state->ctx, ctx, cmd_getpwent_recv_pwnam,
81                         state);
82         } else {
83         /* If there is no valid user left, call libnet_UserList to get a new
84          * list of users. */
85                 composite_error(state->ctx, NT_STATUS_NO_MORE_ENTRIES);
86         }
87         return result;
88 }
89
90 static void cmd_getpwent_recv_pwnam(struct composite_context *ctx)
91 {
92         struct cmd_getpwent_state *state =
93                 talloc_get_type(ctx->async.private_data,
94                                 struct cmd_getpwent_state);
95         struct winbindd_pw *pw;
96
97         DEBUG(5, ("cmd_getpwent_recv_pwnam called\n"));
98
99         state->ctx->status = wb_cmd_getpwnam_recv(ctx, state, &pw);
100         if (!composite_is_ok(state->ctx)) return;
101
102         /*FIXME: Cheat for now and only get one user per call */
103         state->result = pw;
104
105         composite_done(state->ctx);
106 }
107
108 NTSTATUS wb_cmd_getpwent_recv(struct composite_context *ctx,
109                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw,
110                 uint32_t *num_users)
111 {
112         NTSTATUS status = composite_wait(ctx);
113
114         DEBUG(5, ("wb_cmd_getpwent_recv called\n"));
115
116         if (NT_STATUS_IS_OK(status)) {
117                 struct cmd_getpwent_state *state =
118                         talloc_get_type(ctx->private_data,
119                                         struct cmd_getpwent_state);
120                 *pw = talloc_steal(mem_ctx, state->result);
121                 /*FIXME: Cheat and only get oner user */
122                 *num_users = 1;
123         }
124
125         talloc_free(ctx);
126         return status;
127 }
128