s4-winbindd: implement WINBINDD_SIDS_TO_XIDS
[nivanova/samba-autobuild/.git] / source4 / winbind / wb_samba3_protocol.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main winbindd samba3 server routines
4
5    Copyright (C) Stefan Metzmacher      2005
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 "winbind/wb_server.h"
24 #include "smbd/service_stream.h"
25 #include "lib/stream/packet.h"
26 #include "lib/tsocket/tsocket.h"
27
28 /*
29   work out if a packet is complete for protocols that use a 32 bit host byte
30   order length
31 */
32 NTSTATUS wbsrv_samba3_packet_full_request(void *private_data, DATA_BLOB blob, size_t *size)
33 {
34         uint32_t *len;
35         struct winbindd_request *req;
36
37         if (blob.length < 4) {
38                 return STATUS_MORE_ENTRIES;
39         }
40         len = (uint32_t *)blob.data;
41         *size = (*len);
42         if (*size > blob.length) {
43                 return STATUS_MORE_ENTRIES;
44         }
45         if (*size < sizeof(req)) {
46                 /* its not a valid winbind packet. We need to accept
47                    it here, and wbsrv_samba3_pull_request() will throw
48                    it away */
49                 return NT_STATUS_OK;
50         }
51
52         /* now we need to cope with possible extra_data, which is
53            stuck on the end with no length prefix! This is a very very
54            stupid protocol */
55         req = (struct winbindd_request *)blob.data;
56         *size = (*len) + req->extra_len;
57         if (*size > blob.length) {
58                 return STATUS_MORE_ENTRIES;
59         }
60         return NT_STATUS_OK;
61 }
62
63
64 NTSTATUS wbsrv_samba3_pull_request(struct wbsrv_samba3_call *call)
65 {
66         if (call->in.length < sizeof(*call->request)) {
67                 DEBUG(0,("wbsrv_samba3_pull_request: invalid blob length %lu should be %lu\n"
68                          " make sure you use the correct winbind client tools!\n",
69                          (long)call->in.length, (long)sizeof(*call->request)));
70                 return NT_STATUS_INVALID_PARAMETER;
71         }
72
73         call->request = talloc_zero(call, struct winbindd_request);
74         NT_STATUS_HAVE_NO_MEMORY(call->request);
75
76         /* the packet layout is the same as the in memory layout of the request, so just copy it */
77         memcpy(call->request, call->in.data, sizeof(*call->request));
78
79         if (call->in.length != sizeof(*call->request) + call->request->extra_len) {
80                 DEBUG(0,(__location__ " : invalid extra_len %u should be %u\n",
81                          call->request->extra_len, (unsigned)(call->in.length - sizeof(*call->request))));
82                 return NT_STATUS_INVALID_PARAMETER;
83         }
84
85         /* there may be extra data */
86         if (call->request->extra_len != 0) {
87                 call->request->extra_data.data = talloc_size(call->request, call->request->extra_len+1);
88                 NT_STATUS_HAVE_NO_MEMORY(call->request->extra_data.data);
89                 /* guarantee a nul termination, as many of the uses of
90                    this field is for strings */
91                 memcpy(call->request->extra_data.data, call->in.data + sizeof(*call->request),
92                        call->request->extra_len);
93                 call->request->extra_data.data[call->request->extra_len] = 0;
94         } else {
95                 call->request->extra_data.data = NULL;
96         }
97
98         return NT_STATUS_OK;
99 }
100
101 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
102 {
103         DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request->cmd));
104
105         s3call->response = talloc_zero(s3call, struct winbindd_response);
106         NT_STATUS_HAVE_NO_MEMORY(s3call->request);
107
108         s3call->response->length = sizeof(*s3call->response);
109
110         switch(s3call->request->cmd) {
111         case WINBINDD_INTERFACE_VERSION:
112                 return wbsrv_samba3_interface_version(s3call);
113
114         case WINBINDD_CHECK_MACHACC:
115                 return wbsrv_samba3_check_machacc(s3call);
116
117         case WINBINDD_PING:
118                 return wbsrv_samba3_ping(s3call);
119
120         case WINBINDD_INFO:
121                 return wbsrv_samba3_info(s3call);
122
123         case WINBINDD_DOMAIN_NAME:
124                 return wbsrv_samba3_domain_name(s3call);
125
126         case WINBINDD_NETBIOS_NAME:
127                 return wbsrv_samba3_netbios_name(s3call);
128
129         case WINBINDD_PRIV_PIPE_DIR:
130                 return wbsrv_samba3_priv_pipe_dir(s3call);
131
132         case WINBINDD_LOOKUPNAME:
133                 return wbsrv_samba3_lookupname(s3call);
134
135         case WINBINDD_LOOKUPSID:
136                 return wbsrv_samba3_lookupsid(s3call);
137
138         case WINBINDD_PAM_AUTH:
139                 return wbsrv_samba3_pam_auth(s3call);
140
141         case WINBINDD_PAM_AUTH_CRAP:
142                 return wbsrv_samba3_pam_auth_crap(s3call);
143
144         case WINBINDD_GETDCNAME:
145                 return wbsrv_samba3_getdcname(s3call);
146
147         case WINBINDD_GETUSERDOMGROUPS:
148                 return wbsrv_samba3_userdomgroups(s3call);
149
150         case WINBINDD_GETUSERSIDS:
151                 return wbsrv_samba3_usersids(s3call);
152
153         case WINBINDD_LIST_GROUPS:
154                 return wbsrv_samba3_list_groups(s3call);
155
156         case WINBINDD_LIST_TRUSTDOM:
157                 return wbsrv_samba3_list_trustdom(s3call);
158
159         case WINBINDD_LIST_USERS:
160                 return wbsrv_samba3_list_users(s3call);
161
162         case WINBINDD_GETPWNAM:
163                 return wbsrv_samba3_getpwnam(s3call);
164
165         case WINBINDD_GETPWUID:
166                 return wbsrv_samba3_getpwuid(s3call);
167
168         case WINBINDD_SETPWENT:
169                 return wbsrv_samba3_setpwent(s3call);
170
171         case WINBINDD_GETPWENT:
172                 return wbsrv_samba3_getpwent(s3call);
173
174         case WINBINDD_ENDPWENT:
175                 return wbsrv_samba3_endpwent(s3call);
176
177         case WINBINDD_GETGRNAM:
178                 return wbsrv_samba3_getgrnam(s3call);
179
180         case WINBINDD_GETGRGID:
181                 return wbsrv_samba3_getgrgid(s3call);
182
183         case WINBINDD_GETGROUPS:
184                 return wbsrv_samba3_getgroups(s3call);
185
186         case WINBINDD_SETGRENT:
187                 return wbsrv_samba3_setgrent(s3call);
188
189         case WINBINDD_GETGRENT:
190                 return wbsrv_samba3_getgrent(s3call);
191
192         case WINBINDD_ENDGRENT:
193                 return wbsrv_samba3_endgrent(s3call);
194
195         case WINBINDD_SID_TO_UID:
196         case WINBINDD_DUAL_SID2UID:
197                 return wbsrv_samba3_sid2uid(s3call);
198
199         case WINBINDD_SID_TO_GID:
200         case WINBINDD_DUAL_SID2GID:
201                 return wbsrv_samba3_sid2gid(s3call);
202
203         case WINBINDD_UID_TO_SID:
204         case WINBINDD_DUAL_UID2SID:
205                 return wbsrv_samba3_uid2sid(s3call);
206
207         case WINBINDD_GID_TO_SID:
208         case WINBINDD_DUAL_GID2SID:
209                 return wbsrv_samba3_gid2sid(s3call);
210
211         case WINBINDD_DOMAIN_INFO:
212                 return wbsrv_samba3_domain_info(s3call);
213
214         case WINBINDD_PAM_LOGOFF:
215                 return wbsrv_samba3_pam_logoff(s3call);
216
217         case WINBINDD_SIDS_TO_XIDS:
218                 return wbsrv_samba3_sids2xids(s3call);
219
220         /* Unimplemented commands */
221         case WINBINDD_GETPWSID:
222         case WINBINDD_PAM_CHAUTHTOK:
223         case WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP:
224         case WINBINDD_LOOKUPRIDS:
225         case WINBINDD_LOOKUPSIDS:
226         case WINBINDD_ALLOCATE_UID:
227         case WINBINDD_ALLOCATE_GID:
228         case WINBINDD_SHOW_SEQUENCE:
229         case WINBINDD_WINS_BYIP:
230         case WINBINDD_WINS_BYNAME:
231         case WINBINDD_GETGRLST:
232         case WINBINDD_GETSIDALIASES:
233         case WINBINDD_DSGETDCNAME:
234         case WINBINDD_INIT_CONNECTION:
235         case WINBINDD_DUAL_SIDS2XIDS:
236         case WINBINDD_DUAL_USERINFO:
237         case WINBINDD_DUAL_GETSIDALIASES:
238         case WINBINDD_DUAL_NDRCMD:
239         case WINBINDD_CCACHE_NTLMAUTH:
240         case WINBINDD_NUM_CMDS:
241         case WINBINDD_CHANGE_MACHACC:
242         case WINBINDD_PING_DC:
243         case WINBINDD_DC_INFO:
244         case WINBINDD_CCACHE_SAVE:
245                 DEBUG(10, ("Unimplemented winbind samba3 request %d\n",
246                            s3call->request->cmd));
247                 break;
248         }
249
250         s3call->response->result = WINBINDD_ERROR;
251         return NT_STATUS_OK;
252 }
253
254 static NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_samba3_call *call)
255 {
256         uint8_t *extra_data;
257         size_t extra_data_len = 0;
258
259         extra_data = (uint8_t *)call->response->extra_data.data;
260         if (extra_data != NULL) {
261                 extra_data_len = call->response->length -
262                         sizeof(*call->response);
263         }
264
265         call->out = data_blob_talloc(call, NULL, call->response->length);
266         NT_STATUS_HAVE_NO_MEMORY(call->out.data);
267
268         /* don't push real pointer values into sockets */
269         if (extra_data) {
270                 call->response->extra_data.data = (void *)0xFFFFFFFF;
271         }
272
273         memcpy(call->out.data, call->response, sizeof(*call->response));
274         /* set back the pointer */
275         call->response->extra_data.data = extra_data;
276
277         if (extra_data) {
278                 memcpy(call->out.data + sizeof(*call->response),
279                        extra_data,
280                        extra_data_len);
281         }
282
283         return NT_STATUS_OK;
284 }
285
286 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq);
287
288 /*
289  * queue a wbsrv_call reply on a wbsrv_connection
290  * NOTE: that this implies talloc_free(call),
291  *       use talloc_reference(call) if you need it after
292  *       calling wbsrv_queue_reply
293  */
294 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
295 {
296         struct wbsrv_connection *wbsrv_conn = call->wbconn;
297         struct tevent_req *subreq;
298         NTSTATUS status;
299
300         status = wbsrv_samba3_push_reply(call);
301         NT_STATUS_NOT_OK_RETURN(status);
302
303         call->out_iov[0].iov_base = (char *) call->out.data;
304         call->out_iov[0].iov_len = call->out.length;
305
306         subreq = tstream_writev_queue_send(call,
307                                            wbsrv_conn->conn->event.ctx,
308                                            wbsrv_conn->tstream,
309                                            wbsrv_conn->send_queue,
310                                            call->out_iov, 1);
311         if (subreq == NULL) {
312                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
313                                 "no memory for tstream_writev_queue_send");
314                 return NT_STATUS_NO_MEMORY;
315         }
316         tevent_req_set_callback(subreq, wbsrv_samba3_send_reply_done, call);
317
318         return status;
319 }
320
321 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq)
322 {
323         struct wbsrv_samba3_call *call = tevent_req_callback_data(subreq,
324                         struct wbsrv_samba3_call);
325         int sys_errno;
326         int rc;
327
328         rc = tstream_writev_queue_recv(subreq, &sys_errno);
329         TALLOC_FREE(subreq);
330         if (rc == -1) {
331                 const char *reason;
332
333                 reason = talloc_asprintf(call, "wbsrv_samba3_send_reply_done: "
334                                          "tstream_writev_queue_recv() - %d:%s",
335                                          sys_errno, strerror(sys_errno));
336                 if (reason == NULL) {
337                         reason = "wbsrv_samba3_send_reply_done: "
338                                  "tstream_writev_queue_recv() failed";
339                 }
340
341                 wbsrv_terminate_connection(call->wbconn, reason);
342                 return;
343         }
344
345         talloc_free(call);
346 }
347
348 NTSTATUS wbsrv_samba3_process(struct wbsrv_samba3_call *call)
349 {
350         NTSTATUS status;
351
352         status = wbsrv_samba3_pull_request(call);
353         
354         if (!NT_STATUS_IS_OK(status)) {
355                 return status;
356         }
357
358         status = wbsrv_samba3_handle_call(call);
359
360         if (!NT_STATUS_IS_OK(status)) {
361                 talloc_free(call);
362                 return status;
363         }
364
365         if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
366                 return NT_STATUS_OK;
367         }
368
369         status = wbsrv_samba3_send_reply(call);
370         return status;
371 }
372