s4-idmap: Add mapping using uidNumber and gidNumber like idmap_ad
[samba.git] / source4 / winbind / wb_sid2domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Find and init a domain struct for a SID
5
6    Copyright (C) Volker Lendecke 2005
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 "libcli/security/security.h"
27 #include "../lib/util/dlinklist.h"
28 #include "param/param.h"
29
30 static struct wbsrv_domain *find_domain_from_sid(struct wbsrv_service *service,
31                                                  const struct dom_sid *sid)
32 {
33         struct wbsrv_domain *domain;
34
35         for (domain = service->domains; domain!=NULL; domain = domain->next) {
36                 if (dom_sid_equal(domain->info->sid, sid)) {
37                         break;
38                 }
39                 if (dom_sid_in_domain(domain->info->sid, sid)) {
40                         break;
41                 }
42         }
43         return domain;
44 }
45
46 struct sid2domain_state {
47         struct composite_context *ctx;
48         struct wbsrv_service *service;
49         struct dom_sid *sid;
50
51         struct wbsrv_domain *domain;
52 };
53
54 static void sid2domain_recv_dom_info(struct composite_context *ctx);
55 static void sid2domain_recv_name(struct composite_context *ctx);
56 static void sid2domain_recv_trusted_dom_info(struct composite_context *ctx);
57 static void sid2domain_recv_init(struct composite_context *ctx);
58
59 struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
60                                              struct wbsrv_service *service,
61                                              const struct dom_sid *sid)
62 {
63         struct composite_context *result, *ctx;
64         struct sid2domain_state *state;
65         DEBUG(5, ("wb_sid2domain_send called\n"));
66         result = composite_create(mem_ctx, service->task->event_ctx);
67         if (result == NULL) goto failed;
68
69         state = talloc(result, struct sid2domain_state);
70         if (state == NULL) goto failed;
71         state->ctx = result;
72         result->private_data = state;
73
74         state->service = service;
75         state->sid = dom_sid_dup(state, sid);
76         if (state->sid == NULL) goto failed;
77
78         state->domain = find_domain_from_sid(service, sid);
79         if (state->domain != NULL) {
80                 result->status = NT_STATUS_OK;
81                 composite_done(result);
82                 return result;
83         }
84
85         if (dom_sid_equal(service->primary_sid, sid) ||
86             dom_sid_in_domain(service->primary_sid, sid)) {
87                 ctx = wb_get_dom_info_send(state, service,
88                                            lpcfg_workgroup(service->task->lp_ctx),
89                                            lpcfg_realm(service->task->lp_ctx),
90                                            service->primary_sid);
91                 if (ctx == NULL) goto failed;
92                 ctx->async.fn = sid2domain_recv_dom_info;
93                 ctx->async.private_data = state;
94                 return result;
95         }
96
97         ctx = wb_cmd_lookupsid_send(state, service, state->sid);
98         if (ctx == NULL) goto failed;
99         composite_continue(result, ctx, sid2domain_recv_name, state);
100
101         return result;
102
103  failed:
104         talloc_free(result);
105         return NULL;
106
107 }
108
109 static void sid2domain_recv_dom_info(struct composite_context *ctx)
110 {
111         struct sid2domain_state *state =
112                 talloc_get_type(ctx->async.private_data,
113                                 struct sid2domain_state);
114         struct wb_dom_info *info;
115
116         state->ctx->status = wb_get_dom_info_recv(ctx, state, &info);
117         if (!composite_is_ok(state->ctx)) return;
118
119         ctx = wb_init_domain_send(state, state->service, info);
120
121         composite_continue(state->ctx, ctx, sid2domain_recv_init, state);
122 }
123
124 static void sid2domain_recv_name(struct composite_context *ctx)
125 {
126         struct sid2domain_state *state =
127                 talloc_get_type(ctx->async.private_data,
128                                 struct sid2domain_state);
129         struct wb_sid_object *name;
130
131         state->ctx->status = wb_cmd_lookupsid_recv(ctx, state, &name);
132         if (!composite_is_ok(state->ctx)) return;
133
134         if (name->type == SID_NAME_UNKNOWN) {
135                 composite_error(state->ctx, NT_STATUS_NO_SUCH_DOMAIN);
136                 return;
137         }
138
139         if (name->type != SID_NAME_DOMAIN) {
140                 state->sid->num_auths -= 1;
141         }
142
143         ctx = wb_trusted_dom_info_send(state, state->service, name->domain,
144                                        state->sid);
145
146         composite_continue(state->ctx, ctx, sid2domain_recv_trusted_dom_info,
147                            state);
148 }
149
150 static void sid2domain_recv_trusted_dom_info(struct composite_context *ctx)
151 {
152         struct sid2domain_state *state =
153                 talloc_get_type(ctx->async.private_data,
154                                 struct sid2domain_state);
155         struct wb_dom_info *info;
156
157         state->ctx->status = wb_trusted_dom_info_recv(ctx, state, &info);
158         if (!composite_is_ok(state->ctx)) return;
159
160         ctx = wb_init_domain_send(state, state->service, info);
161
162         composite_continue(state->ctx, ctx, sid2domain_recv_init, state);
163 }
164
165 static void sid2domain_recv_init(struct composite_context *ctx)
166 {
167         struct sid2domain_state *state =
168                 talloc_get_type(ctx->async.private_data,
169                                 struct sid2domain_state);
170         struct wbsrv_domain *existing;
171
172         state->ctx->status = wb_init_domain_recv(ctx, state,
173                                                  &state->domain);
174         if (!composite_is_ok(state->ctx)) {
175                 DEBUG(10, ("Could not init domain\n"));
176                 return;
177         }
178
179         existing = find_domain_from_sid(state->service, state->sid);
180         if (existing != NULL) {
181                 DEBUG(5, ("Initialized domain twice, dropping second one\n"));
182                 talloc_free(state->domain);
183                 state->domain = existing;
184         }
185
186         talloc_steal(state->service, state->domain);
187         DLIST_ADD(state->service->domains, state->domain);
188
189         composite_done(state->ctx);
190 }
191
192 NTSTATUS wb_sid2domain_recv(struct composite_context *ctx,
193                             struct wbsrv_domain **result)
194 {
195         NTSTATUS status = composite_wait(ctx);
196         if (NT_STATUS_IS_OK(status)) {
197                 struct sid2domain_state *state =
198                         talloc_get_type(ctx->private_data,
199                                         struct sid2domain_state);
200                 *result = state->domain;
201         }
202         talloc_free(ctx);
203         return status;
204 }
205
206 NTSTATUS wb_sid2domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
207                        const struct dom_sid *sid,
208                        struct wbsrv_domain **result)
209 {
210         struct composite_context *c = wb_sid2domain_send(mem_ctx, service,
211                                                          sid);
212         return wb_sid2domain_recv(c, result);
213 }