r23653: add dummy implementation for all nss calls to let nsstest
[metze/samba/wip.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 "nsswitch/winbind_nss_config.h"
25 #include "nsswitch/winbindd_nss.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_stream.h"
28 #include "lib/stream/packet.h"
29
30 /*
31   work out if a packet is complete for protocols that use a 32 bit host byte
32   order length
33 */
34 NTSTATUS wbsrv_samba3_packet_full_request(void *private, DATA_BLOB blob, size_t *size)
35 {
36         uint32_t *len;
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         return NT_STATUS_OK;
46 }
47
48
49 NTSTATUS wbsrv_samba3_pull_request(DATA_BLOB blob, struct wbsrv_connection *wbconn,
50                                    struct wbsrv_samba3_call **_call)
51 {
52         struct wbsrv_samba3_call *call;
53
54         if (blob.length != sizeof(call->request)) {
55                 DEBUG(0,("wbsrv_samba3_pull_request: invalid blob length %lu should be %lu\n"
56                          " make sure you use the correct winbind client tools!\n",
57                          (long)blob.length, (long)sizeof(call->request)));
58                 return NT_STATUS_INVALID_PARAMETER;
59         }
60
61         call = talloc_zero(wbconn, struct wbsrv_samba3_call);
62         NT_STATUS_HAVE_NO_MEMORY(call);
63
64         /* the packet layout is the same as the in memory layout of the request, so just copy it */
65         memcpy(&call->request, blob.data, sizeof(call->request));
66
67         call->wbconn = wbconn;
68         call->event_ctx = call->wbconn->conn->event.ctx;
69         
70         *_call = call;
71         return NT_STATUS_OK;
72 }
73
74 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
75 {
76         DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request.cmd));
77
78         s3call->response.length = sizeof(s3call->response);
79
80         switch(s3call->request.cmd) {
81         case WINBINDD_INTERFACE_VERSION:
82                 return wbsrv_samba3_interface_version(s3call);
83
84 #if 0
85         case WINBINDD_CHECK_MACHACC:
86                 return wbsrv_samba3_check_machacc(s3call);
87 #endif
88
89         case WINBINDD_PING:
90                 return wbsrv_samba3_ping(s3call);
91
92         case WINBINDD_INFO:
93                 return wbsrv_samba3_info(s3call);
94
95         case WINBINDD_DOMAIN_NAME:
96                 return wbsrv_samba3_domain_name(s3call);
97
98         case WINBINDD_NETBIOS_NAME:
99                 return wbsrv_samba3_netbios_name(s3call);
100
101         case WINBINDD_PRIV_PIPE_DIR:
102                 return wbsrv_samba3_priv_pipe_dir(s3call);
103
104         case WINBINDD_LOOKUPNAME:
105                 return wbsrv_samba3_lookupname(s3call);
106
107         case WINBINDD_LOOKUPSID:
108                 return wbsrv_samba3_lookupsid(s3call);
109
110         case WINBINDD_PAM_AUTH:
111                 return wbsrv_samba3_pam_auth(s3call);
112
113         case WINBINDD_PAM_AUTH_CRAP:
114                 return wbsrv_samba3_pam_auth_crap(s3call);
115
116         case WINBINDD_GETDCNAME:
117                 return wbsrv_samba3_getdcname(s3call);
118
119         case WINBINDD_GETUSERDOMGROUPS:
120                 return wbsrv_samba3_userdomgroups(s3call);
121
122         case WINBINDD_GETUSERSIDS:
123                 return wbsrv_samba3_usersids(s3call);
124
125         case WINBINDD_LIST_TRUSTDOM:
126                 return wbsrv_samba3_list_trustdom(s3call);
127
128         case WINBINDD_GETPWNAM:
129                 return wbsrv_samba3_getpwnam(s3call);
130
131         case WINBINDD_GETPWUID:
132                 return wbsrv_samba3_getpwuid(s3call);
133
134         case WINBINDD_SETPWENT:
135                 return wbsrv_samba3_setpwent(s3call);
136
137         case WINBINDD_GETPWENT:
138                 return wbsrv_samba3_getpwent(s3call);
139
140         case WINBINDD_ENDPWENT:
141                 return wbsrv_samba3_endpwent(s3call);
142
143         case WINBINDD_GETGRNAM:
144                 return wbsrv_samba3_getgrnam(s3call);
145
146         case WINBINDD_GETGRGID:
147                 return wbsrv_samba3_getgrgid(s3call);
148
149         case WINBINDD_GETGROUPS:
150                 return wbsrv_samba3_getgroups(s3call);
151
152         case WINBINDD_SETGRENT:
153                 return wbsrv_samba3_setgrent(s3call);
154
155         case WINBINDD_GETGRENT:
156                 return wbsrv_samba3_getgrent(s3call);
157
158         case WINBINDD_ENDGRENT:
159                 return wbsrv_samba3_endgrent(s3call);
160
161                 /* Unimplemented commands */
162
163         case WINBINDD_PAM_CHAUTHTOK:
164         case WINBINDD_PAM_LOGOFF:
165         case WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP:
166         case WINBINDD_LIST_USERS:
167         case WINBINDD_LIST_GROUPS:
168         case WINBINDD_LOOKUPRIDS:
169         case WINBINDD_SID_TO_UID:
170         case WINBINDD_SID_TO_GID:
171         case WINBINDD_SIDS_TO_XIDS:
172         case WINBINDD_UID_TO_SID:
173         case WINBINDD_GID_TO_SID:
174         case WINBINDD_ALLOCATE_UID:
175         case WINBINDD_ALLOCATE_GID:
176         case WINBINDD_SET_MAPPING:
177         case WINBINDD_SET_HWM:
178         case WINBINDD_DUMP_MAPS:
179         case WINBINDD_CHECK_MACHACC:
180         case WINBINDD_DOMAIN_INFO:
181         case WINBINDD_SHOW_SEQUENCE:
182         case WINBINDD_WINS_BYIP:
183         case WINBINDD_WINS_BYNAME:
184         case WINBINDD_GETGRLST:
185         case WINBINDD_INIT_CONNECTION:
186         case WINBINDD_DUAL_SID2UID:
187         case WINBINDD_DUAL_SID2GID:
188         case WINBINDD_DUAL_SIDS2XIDS:
189         case WINBINDD_DUAL_UID2SID:
190         case WINBINDD_DUAL_GID2SID:
191         case WINBINDD_DUAL_SET_MAPPING:
192         case WINBINDD_DUAL_SET_HWM:
193         case WINBINDD_DUAL_DUMP_MAPS:
194         case WINBINDD_DUAL_UID2NAME:
195         case WINBINDD_DUAL_NAME2UID:
196         case WINBINDD_DUAL_GID2NAME:
197         case WINBINDD_DUAL_NAME2GID:
198         case WINBINDD_DUAL_USERINFO:
199         case WINBINDD_DUAL_GETSIDALIASES:
200         case WINBINDD_CCACHE_NTLMAUTH:
201         case WINBINDD_NUM_CMDS:
202                 DEBUG(10, ("Unimplemented winbind samba3 request %d\n", 
203                            s3call->request.cmd));
204                 break;
205         }
206
207         s3call->response.result = WINBINDD_ERROR;
208         return NT_STATUS_OK;
209 }
210
211 static NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_samba3_call *call, TALLOC_CTX *mem_ctx, DATA_BLOB *_blob)
212 {
213         DATA_BLOB blob;
214         uint8_t *extra_data;
215         size_t extra_data_len = 0;
216
217         extra_data = call->response.extra_data.data;
218         if (extra_data) {
219                 extra_data_len = call->response.length -
220                         sizeof(call->response);
221         }
222
223         blob = data_blob_talloc(mem_ctx, NULL, call->response.length);
224         NT_STATUS_HAVE_NO_MEMORY(blob.data);
225
226         /* don't push real pointer values into sockets */
227         if (extra_data) {
228                 call->response.extra_data.data = (void *)0xFFFFFFFF;
229         }
230         memcpy(blob.data, &call->response, sizeof(call->response));
231         /* set back the pointer */
232         call->response.extra_data.data = extra_data;
233
234         if (extra_data) {
235                 memcpy(blob.data + sizeof(call->response), extra_data, extra_data_len);
236         }
237
238         *_blob = blob;
239         return NT_STATUS_OK;
240 }
241
242 /*
243  * queue a wbsrv_call reply on a wbsrv_connection
244  * NOTE: that this implies talloc_free(call),
245  *       use talloc_reference(call) if you need it after
246  *       calling wbsrv_queue_reply
247  */
248 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
249 {
250         struct wbsrv_connection *wbconn = call->wbconn;
251         DATA_BLOB rep;
252         NTSTATUS status;
253
254         status = wbsrv_samba3_push_reply(call, call, &rep);
255         NT_STATUS_NOT_OK_RETURN(status);
256
257         status = packet_send(call->wbconn->packet, rep);
258         
259         talloc_free(call);
260
261         if (!NT_STATUS_IS_OK(status)) {
262                 wbsrv_terminate_connection(wbconn,
263                                            "failed to packet_send winbindd reply");
264                 return status;
265         }
266         /* the call isn't needed any more */
267         return status;
268 }
269
270 NTSTATUS wbsrv_samba3_process(void *private, DATA_BLOB blob)
271 {
272         NTSTATUS status;
273         struct wbsrv_connection *wbconn = talloc_get_type(private, 
274                                                           struct wbsrv_connection);
275         struct wbsrv_samba3_call *call;
276         status = wbsrv_samba3_pull_request(blob, wbconn, &call);
277         
278         if (!NT_STATUS_IS_OK(status)) {
279                 return status;
280         }
281         
282         status = wbsrv_samba3_handle_call(call);
283
284         if (!NT_STATUS_IS_OK(status)) {
285                 talloc_free(call);
286                 return status;
287         }
288
289         if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
290                 return NT_STATUS_OK;
291         }
292
293         status = wbsrv_samba3_send_reply(call);
294         return status;
295 }
296