1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | Command backend for setgrent
|
---|
5 |
|
---|
6 | Copyright (C) Matthieu Patou 2010
|
---|
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_setgrent_state {
|
---|
28 | struct composite_context *ctx;
|
---|
29 | struct wbsrv_service *service;
|
---|
30 | struct libnet_context *libnet_ctx;
|
---|
31 |
|
---|
32 | struct wbsrv_grent *result;
|
---|
33 | char *domain_name;
|
---|
34 | };
|
---|
35 |
|
---|
36 | static void cmd_setgrent_recv_domain(struct composite_context *ctx);
|
---|
37 | static void cmd_setgrent_recv_group_list(struct composite_context *ctx);
|
---|
38 |
|
---|
39 | struct composite_context *wb_cmd_setgrent_send(TALLOC_CTX *mem_ctx,
|
---|
40 | struct wbsrv_service *service)
|
---|
41 | {
|
---|
42 | struct composite_context *ctx, *result;
|
---|
43 | struct cmd_setgrent_state *state;
|
---|
44 |
|
---|
45 | DEBUG(5, ("wb_cmd_setgrent_send called\n"));
|
---|
46 |
|
---|
47 | result = composite_create(mem_ctx, service->task->event_ctx);
|
---|
48 | if (!result) return NULL;
|
---|
49 |
|
---|
50 | state = talloc(mem_ctx, struct cmd_setgrent_state);
|
---|
51 | if (composite_nomem(state, result)) return result;
|
---|
52 |
|
---|
53 | state->ctx = result;
|
---|
54 | result->private_data = state;
|
---|
55 | state->service = service;
|
---|
56 |
|
---|
57 | state->result = talloc(state, struct wbsrv_grent);
|
---|
58 | if (composite_nomem(state->result, state->ctx)) return result;
|
---|
59 |
|
---|
60 | ctx = wb_sid2domain_send(state, service, service->primary_sid);
|
---|
61 | if (composite_nomem(ctx, state->ctx)) return result;
|
---|
62 |
|
---|
63 | composite_continue(state->ctx, ctx, cmd_setgrent_recv_domain, state);
|
---|
64 | return result;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static void cmd_setgrent_recv_domain(struct composite_context *ctx)
|
---|
68 | {
|
---|
69 | struct cmd_setgrent_state *state = talloc_get_type(
|
---|
70 | ctx->async.private_data, struct cmd_setgrent_state);
|
---|
71 | struct wbsrv_domain *domain;
|
---|
72 | struct libnet_GroupList *group_list;
|
---|
73 |
|
---|
74 | DEBUG(5, ("cmd_setgrent_recv_domain called\n"));
|
---|
75 |
|
---|
76 | state->ctx->status = wb_sid2domain_recv(ctx, &domain);
|
---|
77 | if (!composite_is_ok(state->ctx)) return;
|
---|
78 |
|
---|
79 | state->libnet_ctx = domain->libnet_ctx;
|
---|
80 |
|
---|
81 | group_list = talloc(state->result, struct libnet_GroupList);
|
---|
82 | if (composite_nomem(group_list, state->ctx)) return;
|
---|
83 |
|
---|
84 | state->domain_name = talloc_strdup(state,
|
---|
85 | domain->libnet_ctx->samr.name);
|
---|
86 | group_list->in.domain_name = talloc_strdup(state,
|
---|
87 | domain->libnet_ctx->samr.name);
|
---|
88 | if (composite_nomem(group_list->in.domain_name, state->ctx)) return;
|
---|
89 |
|
---|
90 | /* Page size recommended by Rafal */
|
---|
91 | group_list->in.page_size = 128;
|
---|
92 |
|
---|
93 | /* Always get the start of the list */
|
---|
94 | group_list->in.resume_index = 0;
|
---|
95 |
|
---|
96 | ctx = libnet_GroupList_send(domain->libnet_ctx, state->result, group_list,
|
---|
97 | NULL);
|
---|
98 |
|
---|
99 | state->result->page_index = -1;
|
---|
100 | composite_continue(state->ctx, ctx, cmd_setgrent_recv_group_list, state);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void cmd_setgrent_recv_group_list(struct composite_context *ctx)
|
---|
104 | {
|
---|
105 | struct cmd_setgrent_state *state = talloc_get_type(
|
---|
106 | ctx->async.private_data, struct cmd_setgrent_state);
|
---|
107 | struct libnet_GroupList *group_list;
|
---|
108 | struct libnet_GroupList *group_list_send;
|
---|
109 | DEBUG(5, ("cmd_setgrent_recv_group_list called\n"));
|
---|
110 |
|
---|
111 | group_list = talloc(state->result, struct libnet_GroupList);
|
---|
112 | if (composite_nomem(group_list, state->ctx)) return;
|
---|
113 |
|
---|
114 | state->ctx->status = libnet_GroupList_recv(ctx, state->result,
|
---|
115 | group_list);
|
---|
116 | if (NT_STATUS_IS_OK(state->ctx->status) ||
|
---|
117 | NT_STATUS_EQUAL(state->ctx->status, STATUS_MORE_ENTRIES)) {
|
---|
118 | if( state->result->page_index == -1) { /* First run*/
|
---|
119 | state->result->group_list = group_list;
|
---|
120 | state->result->page_index = 0;
|
---|
121 | state->result->libnet_ctx = state->libnet_ctx;
|
---|
122 | } else {
|
---|
123 | int i;
|
---|
124 | struct grouplist *tmp;
|
---|
125 | tmp = state->result->group_list->out.groups;
|
---|
126 | state->result->group_list->out.groups = talloc_realloc(state->result,tmp,struct grouplist,
|
---|
127 | state->result->group_list->out.count+group_list->out.count);
|
---|
128 | tmp = state->result->group_list->out.groups;
|
---|
129 | for(i=0;i<group_list->out.count;i++ ) {
|
---|
130 | tmp[i+state->result->group_list->out.count].groupname = talloc_steal(state->result,group_list->out.groups[i].groupname);
|
---|
131 | }
|
---|
132 | state->result->group_list->out.count += group_list->out.count;
|
---|
133 | talloc_free(group_list);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | if (NT_STATUS_IS_OK(state->ctx->status) ) {
|
---|
138 | composite_done(state->ctx);
|
---|
139 | } else {
|
---|
140 | group_list_send = talloc(state->result, struct libnet_GroupList);
|
---|
141 | if (composite_nomem(group_list_send, state->ctx)) return;
|
---|
142 | group_list_send->in.domain_name = talloc_strdup(state, state->domain_name);
|
---|
143 | group_list_send->in.resume_index = group_list->out.resume_index;
|
---|
144 | group_list_send->in.page_size = 128;
|
---|
145 | ctx = libnet_GroupList_send(state->libnet_ctx, state->result, group_list_send, NULL);
|
---|
146 | composite_continue(state->ctx, ctx, cmd_setgrent_recv_group_list, state);
|
---|
147 | }
|
---|
148 | } else {
|
---|
149 | composite_error(state->ctx, state->ctx->status);
|
---|
150 | }
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | NTSTATUS wb_cmd_setgrent_recv(struct composite_context *ctx,
|
---|
155 | TALLOC_CTX *mem_ctx, struct wbsrv_grent **grent)
|
---|
156 | {
|
---|
157 | NTSTATUS status = composite_wait(ctx);
|
---|
158 |
|
---|
159 | DEBUG(5, ("wb_cmd_setgrent_recv called\n"));
|
---|
160 |
|
---|
161 | if (NT_STATUS_IS_OK(status)) {
|
---|
162 | struct cmd_setgrent_state *state =
|
---|
163 | talloc_get_type(ctx->private_data,
|
---|
164 | struct cmd_setgrent_state);
|
---|
165 |
|
---|
166 | *grent = talloc_steal(mem_ctx, state->result);
|
---|
167 | }
|
---|
168 |
|
---|
169 | talloc_free(ctx);
|
---|
170 | return status;
|
---|
171 | }
|
---|