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