s3-spoolss: implement spoolss_EnumJobs level 3.
[Samba.git] / source3 / winbindd / winbindd_misc.c
blobac8f1a7dfdaee3aea11fddc10a24ecf92e3fbae9
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon - miscellaneous other functions
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Bartlett 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "winbindd.h"
25 #include "../librpc/gen_ndr/cli_netlogon.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 /* Constants and helper functions for determining domain trust types */
32 enum trust_type {
33 EXTERNAL = 0,
34 FOREST,
35 IN_FOREST,
36 NONE,
39 const char *trust_type_strings[] = {"External",
40 "Forest",
41 "In Forest",
42 "None"};
44 static enum trust_type get_trust_type(struct winbindd_tdc_domain *domain)
46 if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN)
47 return EXTERNAL;
48 else if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE)
49 return FOREST;
50 else if (((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) == NETR_TRUST_FLAG_IN_FOREST) &&
51 ((domain->trust_flags & NETR_TRUST_FLAG_PRIMARY) == 0x0))
52 return IN_FOREST;
53 return NONE;
56 static const char *get_trust_type_string(struct winbindd_tdc_domain *domain)
58 return trust_type_strings[get_trust_type(domain)];
61 static bool trust_is_inbound(struct winbindd_tdc_domain *domain)
63 return (domain->trust_flags == 0x0) ||
64 ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
65 NETR_TRUST_FLAG_IN_FOREST) ||
66 ((domain->trust_flags & NETR_TRUST_FLAG_INBOUND) ==
67 NETR_TRUST_FLAG_INBOUND);
70 static bool trust_is_outbound(struct winbindd_tdc_domain *domain)
72 return (domain->trust_flags == 0x0) ||
73 ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
74 NETR_TRUST_FLAG_IN_FOREST) ||
75 ((domain->trust_flags & NETR_TRUST_FLAG_OUTBOUND) ==
76 NETR_TRUST_FLAG_OUTBOUND);
79 static bool trust_is_transitive(struct winbindd_tdc_domain *domain)
81 if ((domain->trust_attribs == NETR_TRUST_ATTRIBUTE_NON_TRANSITIVE) ||
82 (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) ||
83 (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL))
84 return False;
85 return True;
88 void winbindd_list_trusted_domains(struct winbindd_cli_state *state)
90 struct winbindd_tdc_domain *dom_list = NULL;
91 struct winbindd_tdc_domain *d = NULL;
92 size_t num_domains = 0;
93 int extra_data_len = 0;
94 char *extra_data = NULL;
95 int i = 0;
97 DEBUG(3, ("[%5lu]: list trusted domains\n",
98 (unsigned long)state->pid));
100 if( !wcache_tdc_fetch_list( &dom_list, &num_domains )) {
101 request_error(state);
102 goto done;
105 extra_data = talloc_strdup(state->mem_ctx, "");
106 if (extra_data == NULL) {
107 request_error(state);
108 goto done;
111 for ( i = 0; i < num_domains; i++ ) {
112 struct winbindd_domain *domain;
113 bool is_online = true;
115 d = &dom_list[i];
116 domain = find_domain_from_name_noinit(d->domain_name);
117 if (domain) {
118 is_online = domain->online;
120 extra_data = talloc_asprintf_append_buffer(
121 extra_data,
122 "%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s\n",
123 d->domain_name,
124 d->dns_name ? d->dns_name : d->domain_name,
125 sid_string_talloc(state->mem_ctx, &d->sid),
126 get_trust_type_string(d),
127 trust_is_transitive(d) ? "Yes" : "No",
128 trust_is_inbound(d) ? "Yes" : "No",
129 trust_is_outbound(d) ? "Yes" : "No",
130 is_online ? "Online" : "Offline" );
133 extra_data_len = strlen(extra_data);
134 if (extra_data_len > 0) {
136 /* Strip the last \n */
137 extra_data[extra_data_len-1] = '\0';
139 state->response->extra_data.data = extra_data;
140 state->response->length += extra_data_len;
143 request_ok(state);
144 done:
145 TALLOC_FREE( dom_list );
148 enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *domain,
149 struct winbindd_cli_state *state)
151 int i;
152 int extra_data_len = 0;
153 char *extra_data;
154 NTSTATUS result;
155 bool have_own_domain = False;
156 struct netr_DomainTrustList trusts;
158 DEBUG(3, ("[%5lu]: list trusted domains\n",
159 (unsigned long)state->pid));
161 result = domain->methods->trusted_domains(domain, state->mem_ctx,
162 &trusts);
164 if (!NT_STATUS_IS_OK(result)) {
165 DEBUG(3, ("winbindd_dual_list_trusted_domains: trusted_domains returned %s\n",
166 nt_errstr(result) ));
167 return WINBINDD_ERROR;
170 extra_data = talloc_strdup(state->mem_ctx, "");
172 for (i=0; i<trusts.count; i++) {
173 extra_data = talloc_asprintf_append_buffer(
174 extra_data, "%s\\%s\\%s\n",
175 trusts.array[i].netbios_name,
176 trusts.array[i].dns_name,
177 sid_string_talloc(state->mem_ctx,
178 trusts.array[i].sid));
181 /* add our primary domain */
183 for (i=0; i<trusts.count; i++) {
184 if (strequal(trusts.array[i].netbios_name, domain->name)) {
185 have_own_domain = True;
186 break;
190 if (state->request->data.list_all_domains && !have_own_domain) {
191 extra_data = talloc_asprintf_append_buffer(
192 extra_data, "%s\\%s\\%s\n", domain->name,
193 domain->alt_name ? domain->alt_name : domain->name,
194 sid_string_talloc(state->mem_ctx, &domain->sid));
197 extra_data_len = strlen(extra_data);
198 if (extra_data_len > 0) {
200 /* Strip the last \n */
201 extra_data[extra_data_len-1] = '\0';
203 state->response->extra_data.data = extra_data;
204 state->response->length += extra_data_len+1;
207 return WINBINDD_OK;
210 /* This is the child-only version of --sequence. It only allows for a single
211 * domain (ie "our" one) to be displayed. */
213 enum winbindd_result winbindd_dual_show_sequence(struct winbindd_domain *domain,
214 struct winbindd_cli_state *state)
216 DEBUG(3, ("[%5lu]: show sequence\n", (unsigned long)state->pid));
218 /* Ensure null termination */
219 state->request->domain_name[sizeof(state->request->domain_name)-1]='\0';
221 domain->methods->sequence_number(domain, &domain->sequence_number);
223 state->response->data.sequence_number =
224 domain->sequence_number;
226 return WINBINDD_OK;
229 struct domain_info_state {
230 struct winbindd_domain *domain;
231 struct winbindd_cli_state *cli;
232 struct winbindd_request ping_request;
235 static void domain_info_done(struct tevent_req *req);
237 void winbindd_domain_info(struct winbindd_cli_state *cli)
239 struct domain_info_state *state;
240 struct winbindd_domain *domain;
241 struct tevent_req *req;
243 DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
244 cli->request->domain_name));
246 domain = find_domain_from_name_noinit(cli->request->domain_name);
248 if (domain == NULL) {
249 DEBUG(3, ("Did not find domain [%s]\n",
250 cli->request->domain_name));
251 request_error(cli);
252 return;
255 if (domain->initialized) {
256 fstrcpy(cli->response->data.domain_info.name,
257 domain->name);
258 fstrcpy(cli->response->data.domain_info.alt_name,
259 domain->alt_name);
260 sid_to_fstring(cli->response->data.domain_info.sid,
261 &domain->sid);
262 cli->response->data.domain_info.native_mode =
263 domain->native_mode;
264 cli->response->data.domain_info.active_directory =
265 domain->active_directory;
266 cli->response->data.domain_info.primary =
267 domain->primary;
268 request_ok(cli);
269 return;
272 state = talloc_zero(cli->mem_ctx, struct domain_info_state);
273 if (state == NULL) {
274 DEBUG(0, ("talloc failed\n"));
275 request_error(cli);
276 return;
279 state->cli = cli;
280 state->domain = domain;
281 state->ping_request.cmd = WINBINDD_PING;
284 * Send a ping down. This implicitly initializes the domain.
287 req = wb_domain_request_send(state, winbind_event_context(),
288 domain, &state->ping_request);
289 if (req == NULL) {
290 DEBUG(3, ("wb_domain_request_send failed\n"));
291 request_error(cli);
292 return;
294 tevent_req_set_callback(req, domain_info_done, state);
297 static void domain_info_done(struct tevent_req *req)
299 struct domain_info_state *state = tevent_req_callback_data(
300 req, struct domain_info_state);
301 struct winbindd_response *response;
302 int ret, err;
304 ret = wb_domain_request_recv(req, req, &response, &err);
305 TALLOC_FREE(req);
306 if (ret == -1) {
307 DEBUG(10, ("wb_domain_request failed: %s\n", strerror(errno)));
308 request_error(state->cli);
309 return;
311 if (!state->domain->initialized) {
312 DEBUG(5, ("wb_domain_request did not initialize domain %s\n",
313 state->domain->name));
314 request_error(state->cli);
315 return;
318 fstrcpy(state->cli->response->data.domain_info.name,
319 state->domain->name);
320 fstrcpy(state->cli->response->data.domain_info.alt_name,
321 state->domain->alt_name);
322 sid_to_fstring(state->cli->response->data.domain_info.sid,
323 &state->domain->sid);
325 state->cli->response->data.domain_info.native_mode =
326 state->domain->native_mode;
327 state->cli->response->data.domain_info.active_directory =
328 state->domain->active_directory;
329 state->cli->response->data.domain_info.primary =
330 state->domain->primary;
332 request_ok(state->cli);
335 /* List various tidbits of information */
337 void winbindd_info(struct winbindd_cli_state *state)
340 DEBUG(3, ("[%5lu]: request misc info\n", (unsigned long)state->pid));
342 state->response->data.info.winbind_separator = *lp_winbind_separator();
343 fstrcpy(state->response->data.info.samba_version, samba_version_string());
344 request_ok(state);
347 /* Tell the client the current interface version */
349 void winbindd_interface_version(struct winbindd_cli_state *state)
351 DEBUG(3, ("[%5lu]: request interface version\n",
352 (unsigned long)state->pid));
354 state->response->data.interface_version = WINBIND_INTERFACE_VERSION;
355 request_ok(state);
358 /* What domain are we a member of? */
360 void winbindd_domain_name(struct winbindd_cli_state *state)
362 DEBUG(3, ("[%5lu]: request domain name\n", (unsigned long)state->pid));
364 fstrcpy(state->response->data.domain_name, lp_workgroup());
365 request_ok(state);
368 /* What's my name again? */
370 void winbindd_netbios_name(struct winbindd_cli_state *state)
372 DEBUG(3, ("[%5lu]: request netbios name\n",
373 (unsigned long)state->pid));
375 fstrcpy(state->response->data.netbios_name, global_myname());
376 request_ok(state);
379 /* Where can I find the privileged pipe? */
381 void winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
383 char *priv_dir;
384 DEBUG(3, ("[%5lu]: request location of privileged pipe\n",
385 (unsigned long)state->pid));
387 priv_dir = get_winbind_priv_pipe_dir();
388 state->response->extra_data.data = talloc_move(state->mem_ctx,
389 &priv_dir);
391 /* must add one to length to copy the 0 for string termination */
392 state->response->length +=
393 strlen((char *)state->response->extra_data.data) + 1;
395 request_ok(state);