s3: Remove unused winbindd_uid2sid_async
[samba.git] / source3 / winbindd / winbindd_idmap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Async helpers for blocking functions
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Gerald Carter 2006
8    Copyright (C) Simo Sorce 2007
9
10    The helpers always consist of three functions:
11
12    * A request setup function that takes the necessary parameters together
13      with a continuation function that is to be called upon completion
14
15    * A private continuation function that is internal only. This is to be
16      called by the lower-level functions in do_async(). Its only task is to
17      properly call the continuation function named above.
18
19    * A worker function that is called inside the appropriate child process.
20
21    This program is free software; you can redistribute it and/or modify
22    it under the terms of the GNU General Public License as published by
23    the Free Software Foundation; either version 3 of the License, or
24    (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29    GNU General Public License for more details.
30
31    You should have received a copy of the GNU General Public License
32    along with this program.  If not, see <http://www.gnu.org/licenses/>.
33 */
34
35 #include "includes.h"
36 #include "winbindd.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
40
41 static struct winbindd_child static_idmap_child;
42
43 struct winbindd_child *idmap_child(void)
44 {
45         return &static_idmap_child;
46 }
47
48 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
49                                            struct winbindd_cli_state *state)
50 {
51         DOM_SID sid;
52         NTSTATUS result;
53
54         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
55                   state->request->data.dual_sid2id.sid));
56
57         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
58                 DEBUG(1, ("Could not get convert sid %s from string\n",
59                           state->request->data.dual_sid2id.sid));
60                 return WINBINDD_ERROR;
61         }
62
63         result = idmap_sid_to_uid(state->request->domain_name, &sid,
64                                   &state->response->data.uid);
65
66         DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
67                    NT_STATUS_V(result), sid_string_dbg(&sid),
68                    (unsigned int)state->response->data.uid));
69
70         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
71 }
72
73 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
74                                            struct winbindd_cli_state *state)
75 {
76         DOM_SID sid;
77         NTSTATUS result;
78
79         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
80                   state->request->data.dual_sid2id.sid));
81
82         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
83                 DEBUG(1, ("Could not get convert sid %s from string\n",
84                           state->request->data.dual_sid2id.sid));
85                 return WINBINDD_ERROR;
86         }
87
88         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
89
90         result = idmap_sid_to_gid(state->request->domain_name, &sid,
91                                   &state->response->data.gid);
92
93         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
94                    NT_STATUS_V(result), sid_string_dbg(&sid),
95                    (unsigned int)state->response->data.gid));
96
97         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
98 }
99
100 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
101                                            struct winbindd_cli_state *state)
102 {
103         DOM_SID sid;
104         NTSTATUS result;
105
106         DEBUG(3,("[%5lu]: uid to sid %lu\n",
107                  (unsigned long)state->pid,
108                  (unsigned long) state->request->data.uid));
109
110         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
111         result = idmap_uid_to_sid(state->request->domain_name, &sid,
112                                   state->request->data.uid);
113
114         if (NT_STATUS_IS_OK(result)) {
115                 sid_to_fstring(state->response->data.sid.sid, &sid);
116                 state->response->data.sid.type = SID_NAME_USER;
117                 return WINBINDD_OK;
118         }
119
120         return WINBINDD_ERROR;
121 }
122
123 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
124                                   struct winbindd_response *response,
125                                   void *c, void *private_data)
126 {
127         void (*cont)(void *priv, bool succ, const char *sid) =
128                 (void (*)(void *, bool, const char *))c;
129
130         if (!success) {
131                 DEBUG(5, ("Could not trigger gid2sid\n"));
132                 cont(private_data, False, NULL);
133                 return;
134         }
135
136         if (response->result != WINBINDD_OK) {
137                 DEBUG(5, ("gid2sid returned an error\n"));
138                 cont(private_data, False, NULL);
139                 return;
140         }
141
142         cont(private_data, True, response->data.sid.sid);
143 }
144
145 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
146                             void (*cont)(void *private_data, bool success, const char *sid),
147                             void *private_data)
148 {
149         struct winbindd_domain *domain;
150         struct winbindd_request request;
151
152         ZERO_STRUCT(request);
153         request.cmd = WINBINDD_DUAL_GID2SID;
154         request.data.gid = gid;
155
156         for (domain = domain_list(); domain != NULL; domain = domain->next) {
157                 if (domain->have_idmap_config
158                     && (gid >= domain->id_range_low)
159                     && (gid <= domain->id_range_high)) {
160                         fstrcpy(request.domain_name, domain->name);
161                 }
162         }
163
164         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
165                  (void *)cont, private_data);
166 }
167
168 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
169                                            struct winbindd_cli_state *state)
170 {
171         DOM_SID sid;
172         NTSTATUS result;
173
174         DEBUG(3,("[%5lu]: gid %lu to sid\n",
175                 (unsigned long)state->pid,
176                 (unsigned long) state->request->data.gid));
177
178         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
179         result = idmap_gid_to_sid(state->request->domain_name, &sid,
180                                   state->request->data.gid);
181
182         if (NT_STATUS_IS_OK(result)) {
183                 sid_to_fstring(state->response->data.sid.sid, &sid);
184                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
185                            (unsigned long)state->pid,
186                            state->response->data.sid.sid));
187                 state->response->data.sid.type = SID_NAME_DOM_GRP;
188                 return WINBINDD_OK;
189         }
190
191         return WINBINDD_ERROR;
192 }
193
194 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
195         {
196                 .name           = "PING",
197                 .struct_cmd     = WINBINDD_PING,
198                 .struct_fn      = winbindd_dual_ping,
199         },{
200                 .name           = "DUAL_SID2UID",
201                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
202                 .struct_fn      = winbindd_dual_sid2uid,
203         },{
204                 .name           = "DUAL_SID2GID",
205                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
206                 .struct_fn      = winbindd_dual_sid2gid,
207         },{
208                 .name           = "DUAL_UID2SID",
209                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
210                 .struct_fn      = winbindd_dual_uid2sid,
211         },{
212                 .name           = "DUAL_GID2SID",
213                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
214                 .struct_fn      = winbindd_dual_gid2sid,
215         },{
216                 .name           = "NDRCMD",
217                 .struct_cmd     = WINBINDD_DUAL_NDRCMD,
218                 .struct_fn      = winbindd_dual_ndrcmd,
219         },{
220                 .name           = NULL,
221         }
222 };
223
224 void init_idmap_child(void)
225 {
226         setup_child(NULL, &static_idmap_child,
227                     idmap_dispatch_table,
228                     "log.winbindd", "idmap");
229 }