r10438: Move portability functions to lib/replace/; replace now simply ensures
[amitay/samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "smbd/service_stream.h"
25 #include "nsswitch/winbind_nss_config.h"
26 #include "nsswitch/winbindd_nss.h"
27 #include "winbind/wb_server.h"
28 #include "winbind/wb_samba3_protocol.h"
29
30 uint32_t wbsrv_samba3_packet_length(DATA_BLOB blob)
31 {
32         uint32_t *len = (uint32_t *)blob.data;
33         return *len;
34 }
35
36 NTSTATUS wbsrv_samba3_pull_request(DATA_BLOB blob, TALLOC_CTX *mem_ctx, struct wbsrv_call **_call)
37 {
38         struct wbsrv_call *call;
39         struct wbsrv_samba3_call *s3_call;
40
41         if (blob.length != sizeof(s3_call->request)) {
42                 DEBUG(0,("wbsrv_samba3_pull_request: invalid blob length %u should be %u\n"
43                          " make sure you use the correct winbind client tools!\n",
44                          blob.length, sizeof(s3_call->request)));
45                 return NT_STATUS_INVALID_PARAMETER;
46         }
47
48         call = talloc_zero(mem_ctx, struct wbsrv_call);
49         NT_STATUS_HAVE_NO_MEMORY(call);
50
51         s3_call = talloc_zero(call, struct wbsrv_samba3_call);
52         NT_STATUS_HAVE_NO_MEMORY(s3_call);
53         s3_call->call = call;
54
55         /* the packet layout is the same as the in memory layout of the request, so just copy it */
56         memcpy(&s3_call->request, blob.data, sizeof(s3_call->request));
57
58         call->private_data = s3_call;
59
60         *_call = call;
61         return NT_STATUS_OK;
62 }
63
64 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_call *call)
65 {
66         struct wbsrv_samba3_call *s3call = talloc_get_type(call->private_data,
67                                                            struct wbsrv_samba3_call);
68
69         DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request.cmd));
70
71         switch(s3call->request.cmd) {
72         case WINBINDD_INTERFACE_VERSION:
73                 return wbsrv_samba3_interface_version(s3call);
74
75         case WINBINDD_PRIV_PIPE_DIR:
76                 return wbsrv_samba3_priv_pipe_dir(s3call);
77
78         case WINBINDD_PING:
79                 return wbsrv_samba3_ping(s3call);
80         }
81
82         s3call->response.result = WINBINDD_ERROR;
83         return NT_STATUS_OK;
84 }
85
86 NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_call *call, TALLOC_CTX *mem_ctx, DATA_BLOB *_blob)
87 {
88         struct wbsrv_samba3_call *s3call = talloc_get_type(call->private_data,
89                                                            struct wbsrv_samba3_call);
90         DATA_BLOB blob;
91         uint8_t *extra_data;
92         size_t extra_data_len = 0;
93
94         extra_data = s3call->response.extra_data;
95         if (extra_data) {
96                 extra_data_len = strlen((char *)s3call->response.extra_data) + 1;
97         }
98
99         blob = data_blob_talloc(mem_ctx, NULL, sizeof(s3call->response) + extra_data_len);
100         NT_STATUS_HAVE_NO_MEMORY(blob.data);
101
102         /* don't push real pointer values into sockets */
103         if (extra_data) {
104                 s3call->response.extra_data = (void *)0xFFFFFFFF;
105         }
106         s3call->response.length = sizeof(s3call->response) + extra_data_len;
107         memcpy(blob.data, &s3call->response, sizeof(s3call->response));
108         /* set back the pointer */
109         s3call->response.extra_data = extra_data;
110
111         if (extra_data) {
112                 memcpy(blob.data + sizeof(s3call->response), extra_data, extra_data_len);
113         }
114
115         *_blob = blob;
116         return NT_STATUS_OK;
117 }