dns: Use new DNS debugclass in DNS server
[kai/samba.git] / source4 / winbind / wb_name2domain.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Find and init a domain struct for a name
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 #include "winbind/wb_helper.h"
27
28 struct name2domain_state {
29         struct composite_context *ctx;
30         struct wbsrv_service *service;
31
32         struct wbsrv_domain *domain;
33 };
34
35 static void name2domain_recv_sid(struct composite_context *ctx);
36 static void name2domain_recv_domain(struct composite_context *ctx);
37
38 struct composite_context *wb_name2domain_send(TALLOC_CTX *mem_ctx,
39                 struct wbsrv_service *service, const char* name)
40 {
41         struct composite_context *result, *ctx;
42         struct name2domain_state *state;
43         char *user_dom, *user_name;
44         bool ok;
45
46         DEBUG(5, ("wb_name2domain_send called\n"));
47
48         result = composite_create(mem_ctx, service->task->event_ctx);
49         if (!result) return NULL;
50
51         state = talloc(result, struct name2domain_state);
52         if (composite_nomem(state, result)) return result;
53         state->ctx = result;
54         result->private_data = state;
55         state->service = service;
56
57         ok = wb_samba3_split_username(state, service->task->lp_ctx, name, &user_dom, &user_name);
58         if(!ok) {
59                 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
60                 return result;
61         }
62
63         ctx = wb_cmd_lookupname_send(state, service, user_dom, user_name);
64         if (composite_nomem(ctx, state->ctx)) return result;
65
66         composite_continue(result, ctx, name2domain_recv_sid, state);
67         return result;
68 }
69
70 static void name2domain_recv_sid(struct composite_context *ctx)
71 {
72         struct name2domain_state *state =
73                 talloc_get_type(ctx->async.private_data,
74                                 struct name2domain_state);
75         struct wb_sid_object *sid;
76
77         DEBUG(5, ("name2domain_recv_sid called\n"));
78
79         state->ctx->status = wb_cmd_lookupname_recv(ctx, state, &sid);
80         if(!composite_is_ok(state->ctx)) return;
81
82         ctx = wb_sid2domain_send(state, state->service, sid->sid);
83
84         composite_continue(state->ctx, ctx, name2domain_recv_domain, state);
85 }
86
87 static void name2domain_recv_domain(struct composite_context *ctx)
88 {
89         struct name2domain_state *state =
90                 talloc_get_type(ctx->async.private_data,
91                                 struct name2domain_state);
92         struct wbsrv_domain *domain;
93
94         DEBUG(5, ("name2domain_recv_domain called\n"));
95
96         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
97         if(!composite_is_ok(state->ctx)) return;
98
99         state->domain = domain;
100
101         composite_done(state->ctx);
102 }
103
104 NTSTATUS wb_name2domain_recv(struct composite_context *ctx,
105                 struct wbsrv_domain **result)
106 {
107         NTSTATUS status = composite_wait(ctx);
108
109         DEBUG(5, ("wb_name2domain_recv called\n"));
110
111         if (NT_STATUS_IS_OK(status)) {
112                 struct name2domain_state *state =
113                         talloc_get_type(ctx->private_data,
114                                         struct name2domain_state);
115                 *result = state->domain;
116         }
117         talloc_free(ctx);
118         return status;
119 }
120