swrap: Wrap fopen to detect stale file descriptors.
[Samba.git] / source4 / winbind / wb_cmd_setpwent.c
blobab9fd2ef949182cc20f6b2b018650b01cab1030a
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for setpwent
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_setpwent_state {
28 struct composite_context *ctx;
29 struct wbsrv_service *service;
30 struct libnet_context *libnet_ctx;
32 struct wbsrv_pwent *result;
33 char *domain_name;
36 static void cmd_setpwent_recv_domain(struct composite_context *ctx);
37 static void cmd_setpwent_recv_user_list(struct composite_context *ctx);
39 struct composite_context *wb_cmd_setpwent_send(TALLOC_CTX *mem_ctx,
40 struct wbsrv_service *service)
42 struct composite_context *ctx, *result;
43 struct cmd_setpwent_state *state;
45 DEBUG(5, ("wb_cmd_setpwent_send called\n"));
47 result = composite_create(mem_ctx, service->task->event_ctx);
48 if (!result) return NULL;
50 state = talloc(mem_ctx, struct cmd_setpwent_state);
51 if (composite_nomem(state, result)) return result;
53 state->ctx = result;
54 result->private_data = state;
55 state->service = service;
57 state->result = talloc(state, struct wbsrv_pwent);
58 if (composite_nomem(state->result, state->ctx)) return result;
60 ctx = wb_sid2domain_send(state, service, service->primary_sid);
61 if (composite_nomem(ctx, state->ctx)) return result;
63 composite_continue(state->ctx, ctx, cmd_setpwent_recv_domain, state);
64 return result;
67 static void cmd_setpwent_recv_domain(struct composite_context *ctx)
69 struct cmd_setpwent_state *state = talloc_get_type(
70 ctx->async.private_data, struct cmd_setpwent_state);
71 struct wbsrv_domain *domain;
72 struct libnet_UserList *user_list;
74 DEBUG(5, ("cmd_setpwent_recv_domain called\n"));
76 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
77 if (!composite_is_ok(state->ctx)) return;
79 state->libnet_ctx = domain->libnet_ctx;
81 user_list = talloc(state->result, struct libnet_UserList);
82 if (composite_nomem(user_list, state->ctx)) return;
84 state->domain_name = talloc_strdup(state,
85 domain->libnet_ctx->samr.name);
86 user_list->in.domain_name = talloc_strdup(state,
87 domain->libnet_ctx->samr.name);
88 if (composite_nomem(user_list->in.domain_name, state->ctx)) return;
90 /* Page size recommended by Rafal */
91 user_list->in.page_size = 128;
93 /* Always get the start of the list */
94 user_list->in.resume_index = 0;
96 ctx = libnet_UserList_send(domain->libnet_ctx, state->result, user_list,
97 NULL);
99 state->result->page_index = -1;
100 composite_continue(state->ctx, ctx, cmd_setpwent_recv_user_list, state);
103 static void cmd_setpwent_recv_user_list(struct composite_context *ctx)
105 struct cmd_setpwent_state *state = talloc_get_type(
106 ctx->async.private_data, struct cmd_setpwent_state);
107 struct libnet_UserList *user_list;
108 struct libnet_UserList *user_list_send;
109 DEBUG(5, ("cmd_setpwent_recv_user_list called\n"));
111 user_list = talloc(state->result, struct libnet_UserList);
112 if (composite_nomem(user_list, state->ctx)) return;
114 state->ctx->status = libnet_UserList_recv(ctx, state->result,
115 user_list);
116 if (NT_STATUS_IS_OK(state->ctx->status) ||
117 NT_STATUS_EQUAL(state->ctx->status, STATUS_MORE_ENTRIES)) {
119 uint32_t resume_index = user_list->out.resume_index;
121 if (state->result->page_index == -1) { /* First run*/
122 state->result->user_list = user_list;
123 state->result->page_index = 0;
124 state->result->libnet_ctx = state->libnet_ctx;
125 } else {
126 int i, cnt = state->result->user_list->out.count
127 + user_list->out.count;
128 struct userlist *tmp;
129 tmp = state->result->user_list->out.users;
130 state->result->user_list->out.users = talloc_realloc(state->result,
131 tmp, struct userlist,
132 cnt);
133 tmp = state->result->user_list->out.users;
134 for(i=0;i<user_list->out.count;i++ ) {
135 tmp[state->result->user_list->out.count + i].username
136 = talloc_strdup(state->result, user_list->out.users[i].username);
138 state->result->user_list->out.count = cnt;
139 TALLOC_FREE(user_list);
142 if (NT_STATUS_IS_OK(state->ctx->status) ) {
143 composite_done(state->ctx);
144 } else {
145 user_list_send = talloc(state->result, struct libnet_UserList);
146 if (composite_nomem(user_list_send, state->ctx)) return;
147 user_list_send->in.domain_name = talloc_strdup(state, state->domain_name);
148 user_list_send->in.resume_index = resume_index;
149 user_list_send->in.page_size = 128;
150 ctx = libnet_UserList_send(state->libnet_ctx, state->result, user_list_send, NULL);
151 composite_continue(state->ctx, ctx, cmd_setpwent_recv_user_list, state);
153 } else {
154 composite_error(state->ctx, state->ctx->status);
156 return;
159 NTSTATUS wb_cmd_setpwent_recv(struct composite_context *ctx,
160 TALLOC_CTX *mem_ctx, struct wbsrv_pwent **pwent)
162 NTSTATUS status = composite_wait(ctx);
164 DEBUG(5, ("wb_cmd_setpwent_recv called\n"));
166 if (NT_STATUS_IS_OK(status)) {
167 struct cmd_setpwent_state *state =
168 talloc_get_type(ctx->private_data,
169 struct cmd_setpwent_state);
171 *pwent = talloc_steal(mem_ctx, state->result);
174 talloc_free(ctx);
175 return status;