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