s4:winbind Make the 'no SID found' message even more detailed
[abartlet/samba.git/.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 "smbd/service_task.h"
26
27 struct cmd_getpwent_state {
28         struct composite_context *ctx;
29         struct wbsrv_service *service;
30
31         struct wbsrv_pwent *pwent;
32         uint32_t max_users;
33
34         uint32_t num_users;
35         struct winbindd_pw *result;
36 };
37
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
42
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)
46 {
47         struct composite_context *ctx, *result;
48         struct cmd_getpwent_state *state;
49
50         DEBUG(5, ("wb_cmd_getpwent_send called\n"));
51
52         result = composite_create(mem_ctx, service->task->event_ctx);
53         if (!result) return NULL;
54
55         state = talloc(mem_ctx, struct cmd_getpwent_state);
56         if (composite_nomem(state, result)) return result;
57
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;
64
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);
72
73                 pwent->page_index++;
74                 ctx = wb_cmd_getpwnam_send(state, service, username);
75                 if (composite_nomem(ctx, state->ctx)) return result;
76
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);
83         }
84         return result;
85 }
86
87 static void cmd_getpwent_recv_pwnam(struct composite_context *ctx)
88 {
89         struct cmd_getpwent_state *state =
90                 talloc_get_type(ctx->async.private_data,
91                                 struct cmd_getpwent_state);
92         struct winbindd_pw *pw;
93
94         DEBUG(5, ("cmd_getpwent_recv_pwnam called\n"));
95
96         state->ctx->status = wb_cmd_getpwnam_recv(ctx, state, &pw);
97         if (!composite_is_ok(state->ctx)) return;
98
99         /*FIXME: Cheat for now and only get one user per call */
100         state->result = pw;
101
102         composite_done(state->ctx);
103 }
104
105 NTSTATUS wb_cmd_getpwent_recv(struct composite_context *ctx,
106                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw,
107                 uint32_t *num_users)
108 {
109         NTSTATUS status = composite_wait(ctx);
110
111         DEBUG(5, ("wb_cmd_getpwent_recv called\n"));
112
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;
120         }
121
122         talloc_free(ctx);
123         return status;
124 }
125