samba-tool: Fix the IP output of "samba-tool dns serverinfo <some_server>"
[Samba.git] / source4 / winbind / wb_samba3_protocol.c
blob1b78c99c1f942b1cb405490a4b639f0805d00e45
1 /*
2 Unix SMB/CIFS implementation.
3 Main winbindd samba3 server routines
5 Copyright (C) Stefan Metzmacher 2005
6 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
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"
29 work out if a packet is complete for protocols that use a 32 bit host byte
30 order length
32 NTSTATUS wbsrv_samba3_packet_full_request(void *private_data, DATA_BLOB blob, size_t *size)
34 uint32_t *len;
35 struct winbindd_request *req;
37 if (blob.length < 4) {
38 return STATUS_MORE_ENTRIES;
40 len = (uint32_t *)blob.data;
41 *size = (*len);
42 if (*size > blob.length) {
43 return STATUS_MORE_ENTRIES;
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;
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;
60 return NT_STATUS_OK;
64 NTSTATUS wbsrv_samba3_pull_request(struct wbsrv_samba3_call *call)
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;
73 call->request = talloc_zero(call, struct winbindd_request);
74 NT_STATUS_HAVE_NO_MEMORY(call->request);
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));
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;
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;
98 return NT_STATUS_OK;
101 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
103 DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request->cmd));
105 s3call->response = talloc_zero(s3call, struct winbindd_response);
106 NT_STATUS_HAVE_NO_MEMORY(s3call->request);
108 s3call->response->length = sizeof(*s3call->response);
110 switch(s3call->request->cmd) {
111 case WINBINDD_INTERFACE_VERSION:
112 return wbsrv_samba3_interface_version(s3call);
114 case WINBINDD_CHECK_MACHACC:
115 return wbsrv_samba3_check_machacc(s3call);
117 case WINBINDD_PING:
118 return wbsrv_samba3_ping(s3call);
120 case WINBINDD_INFO:
121 return wbsrv_samba3_info(s3call);
123 case WINBINDD_DOMAIN_NAME:
124 return wbsrv_samba3_domain_name(s3call);
126 case WINBINDD_NETBIOS_NAME:
127 return wbsrv_samba3_netbios_name(s3call);
129 case WINBINDD_PRIV_PIPE_DIR:
130 return wbsrv_samba3_priv_pipe_dir(s3call);
132 case WINBINDD_LOOKUPNAME:
133 return wbsrv_samba3_lookupname(s3call);
135 case WINBINDD_LOOKUPSID:
136 return wbsrv_samba3_lookupsid(s3call);
138 case WINBINDD_PAM_AUTH:
139 return wbsrv_samba3_pam_auth(s3call);
141 case WINBINDD_PAM_AUTH_CRAP:
142 return wbsrv_samba3_pam_auth_crap(s3call);
144 case WINBINDD_GETDCNAME:
145 return wbsrv_samba3_getdcname(s3call);
147 case WINBINDD_GETUSERDOMGROUPS:
148 return wbsrv_samba3_userdomgroups(s3call);
150 case WINBINDD_GETUSERSIDS:
151 return wbsrv_samba3_usersids(s3call);
153 case WINBINDD_LIST_GROUPS:
154 return wbsrv_samba3_list_groups(s3call);
156 case WINBINDD_LIST_TRUSTDOM:
157 return wbsrv_samba3_list_trustdom(s3call);
159 case WINBINDD_LIST_USERS:
160 return wbsrv_samba3_list_users(s3call);
162 case WINBINDD_GETPWNAM:
163 return wbsrv_samba3_getpwnam(s3call);
165 case WINBINDD_GETPWUID:
166 return wbsrv_samba3_getpwuid(s3call);
168 case WINBINDD_SETPWENT:
169 return wbsrv_samba3_setpwent(s3call);
171 case WINBINDD_GETPWENT:
172 return wbsrv_samba3_getpwent(s3call);
174 case WINBINDD_ENDPWENT:
175 return wbsrv_samba3_endpwent(s3call);
177 case WINBINDD_GETGRNAM:
178 return wbsrv_samba3_getgrnam(s3call);
180 case WINBINDD_GETGRGID:
181 return wbsrv_samba3_getgrgid(s3call);
183 case WINBINDD_GETGROUPS:
184 return wbsrv_samba3_getgroups(s3call);
186 case WINBINDD_SETGRENT:
187 return wbsrv_samba3_setgrent(s3call);
189 case WINBINDD_GETGRENT:
190 return wbsrv_samba3_getgrent(s3call);
192 case WINBINDD_ENDGRENT:
193 return wbsrv_samba3_endgrent(s3call);
195 case WINBINDD_SID_TO_UID:
196 case WINBINDD_DUAL_SID2UID:
197 return wbsrv_samba3_sid2uid(s3call);
199 case WINBINDD_SID_TO_GID:
200 case WINBINDD_DUAL_SID2GID:
201 return wbsrv_samba3_sid2gid(s3call);
203 case WINBINDD_UID_TO_SID:
204 case WINBINDD_DUAL_UID2SID:
205 return wbsrv_samba3_uid2sid(s3call);
207 case WINBINDD_GID_TO_SID:
208 case WINBINDD_DUAL_GID2SID:
209 return wbsrv_samba3_gid2sid(s3call);
211 case WINBINDD_DOMAIN_INFO:
212 return wbsrv_samba3_domain_info(s3call);
214 case WINBINDD_PAM_LOGOFF:
215 return wbsrv_samba3_pam_logoff(s3call);
217 case WINBINDD_SIDS_TO_XIDS:
218 return wbsrv_samba3_sids2xids(s3call);
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;
250 s3call->response->result = WINBINDD_ERROR;
251 return NT_STATUS_OK;
254 static NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_samba3_call *call)
256 uint8_t *extra_data;
257 size_t extra_data_len = 0;
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);
265 call->out = data_blob_talloc(call, NULL, call->response->length);
266 NT_STATUS_HAVE_NO_MEMORY(call->out.data);
268 /* don't push real pointer values into sockets */
269 if (extra_data) {
270 call->response->extra_data.data = (void *)0xFFFFFFFF;
273 memcpy(call->out.data, call->response, sizeof(*call->response));
274 /* set back the pointer */
275 call->response->extra_data.data = extra_data;
277 if (extra_data) {
278 memcpy(call->out.data + sizeof(*call->response),
279 extra_data,
280 extra_data_len);
283 return NT_STATUS_OK;
286 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq);
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
294 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
296 struct wbsrv_connection *wbsrv_conn = call->wbconn;
297 struct tevent_req *subreq;
298 NTSTATUS status;
300 call->wbconn->pending_calls--;
302 status = wbsrv_samba3_push_reply(call);
303 NT_STATUS_NOT_OK_RETURN(status);
305 call->out_iov[0].iov_base = (char *) call->out.data;
306 call->out_iov[0].iov_len = call->out.length;
308 subreq = tstream_writev_queue_send(call,
309 wbsrv_conn->conn->event.ctx,
310 wbsrv_conn->tstream,
311 wbsrv_conn->send_queue,
312 call->out_iov, 1);
313 if (subreq == NULL) {
314 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
315 "no memory for tstream_writev_queue_send");
316 return NT_STATUS_NO_MEMORY;
318 tevent_req_set_callback(subreq, wbsrv_samba3_send_reply_done, call);
320 return status;
323 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq)
325 struct wbsrv_samba3_call *call = tevent_req_callback_data(subreq,
326 struct wbsrv_samba3_call);
327 int sys_errno;
328 int rc;
330 rc = tstream_writev_queue_recv(subreq, &sys_errno);
331 TALLOC_FREE(subreq);
332 if (rc == -1) {
333 const char *reason;
335 reason = talloc_asprintf(call, "wbsrv_samba3_send_reply_done: "
336 "tstream_writev_queue_recv() - %d:%s",
337 sys_errno, strerror(sys_errno));
338 if (reason == NULL) {
339 reason = "wbsrv_samba3_send_reply_done: "
340 "tstream_writev_queue_recv() failed";
343 wbsrv_terminate_connection(call->wbconn, reason);
344 return;
347 talloc_free(call);
350 NTSTATUS wbsrv_samba3_process(struct wbsrv_samba3_call *call)
352 NTSTATUS status;
354 status = wbsrv_samba3_pull_request(call);
356 if (!NT_STATUS_IS_OK(status)) {
357 return status;
360 call->wbconn->pending_calls++;
362 status = wbsrv_samba3_handle_call(call);
364 if (!NT_STATUS_IS_OK(status)) {
365 call->wbconn->pending_calls--;
366 talloc_free(call);
367 return status;
370 if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
371 return NT_STATUS_OK;
374 status = wbsrv_samba3_send_reply(call);
375 return status;