s4:winbind: stub implementation of WINBINDD_PAM_LOGOFF
[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 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         if (blob.length < 4) {
36                 return STATUS_MORE_ENTRIES;
37         }
38         len = (uint32_t *)blob.data;
39         *size = (*len);
40         if (*size > blob.length) {
41                 return STATUS_MORE_ENTRIES;
42         }
43         return NT_STATUS_OK;
44 }
45
46
47 NTSTATUS wbsrv_samba3_pull_request(struct wbsrv_samba3_call *call)
48 {
49         if (call->in.length != sizeof(call->request)) {
50                 DEBUG(0,("wbsrv_samba3_pull_request: invalid blob length %lu should be %lu\n"
51                          " make sure you use the correct winbind client tools!\n",
52                          (long)call->in.length, (long)sizeof(call->request)));
53                 return NT_STATUS_INVALID_PARAMETER;
54         }
55
56         /* the packet layout is the same as the in memory layout of the request, so just copy it */
57         memcpy(&call->request, call->in.data, sizeof(call->request));
58
59         return NT_STATUS_OK;
60 }
61
62 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
63 {
64         DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request.cmd));
65
66         s3call->response.length = sizeof(s3call->response);
67
68         switch(s3call->request.cmd) {
69         case WINBINDD_INTERFACE_VERSION:
70                 return wbsrv_samba3_interface_version(s3call);
71
72         case WINBINDD_CHECK_MACHACC:
73                 return wbsrv_samba3_check_machacc(s3call);
74
75         case WINBINDD_PING:
76                 return wbsrv_samba3_ping(s3call);
77
78         case WINBINDD_INFO:
79                 return wbsrv_samba3_info(s3call);
80
81         case WINBINDD_DOMAIN_NAME:
82                 return wbsrv_samba3_domain_name(s3call);
83
84         case WINBINDD_NETBIOS_NAME:
85                 return wbsrv_samba3_netbios_name(s3call);
86
87         case WINBINDD_PRIV_PIPE_DIR:
88                 return wbsrv_samba3_priv_pipe_dir(s3call);
89
90         case WINBINDD_LOOKUPNAME:
91                 return wbsrv_samba3_lookupname(s3call);
92
93         case WINBINDD_LOOKUPSID:
94                 return wbsrv_samba3_lookupsid(s3call);
95
96         case WINBINDD_PAM_AUTH:
97                 return wbsrv_samba3_pam_auth(s3call);
98
99         case WINBINDD_PAM_AUTH_CRAP:
100                 return wbsrv_samba3_pam_auth_crap(s3call);
101
102         case WINBINDD_GETDCNAME:
103                 return wbsrv_samba3_getdcname(s3call);
104
105         case WINBINDD_GETUSERDOMGROUPS:
106                 return wbsrv_samba3_userdomgroups(s3call);
107
108         case WINBINDD_GETUSERSIDS:
109                 return wbsrv_samba3_usersids(s3call);
110
111         case WINBINDD_LIST_GROUPS:
112                 return wbsrv_samba3_list_groups(s3call);
113
114         case WINBINDD_LIST_TRUSTDOM:
115                 return wbsrv_samba3_list_trustdom(s3call);
116
117         case WINBINDD_LIST_USERS:
118                 return wbsrv_samba3_list_users(s3call);
119
120         case WINBINDD_GETPWNAM:
121                 return wbsrv_samba3_getpwnam(s3call);
122
123         case WINBINDD_GETPWUID:
124                 return wbsrv_samba3_getpwuid(s3call);
125
126         case WINBINDD_SETPWENT:
127                 return wbsrv_samba3_setpwent(s3call);
128
129         case WINBINDD_GETPWENT:
130                 return wbsrv_samba3_getpwent(s3call);
131
132         case WINBINDD_ENDPWENT:
133                 return wbsrv_samba3_endpwent(s3call);
134
135         case WINBINDD_GETGRNAM:
136                 return wbsrv_samba3_getgrnam(s3call);
137
138         case WINBINDD_GETGRGID:
139                 return wbsrv_samba3_getgrgid(s3call);
140
141         case WINBINDD_GETGROUPS:
142                 return wbsrv_samba3_getgroups(s3call);
143
144         case WINBINDD_SETGRENT:
145                 return wbsrv_samba3_setgrent(s3call);
146
147         case WINBINDD_GETGRENT:
148                 return wbsrv_samba3_getgrent(s3call);
149
150         case WINBINDD_ENDGRENT:
151                 return wbsrv_samba3_endgrent(s3call);
152
153         case WINBINDD_SID_TO_UID:
154         case WINBINDD_DUAL_SID2UID:
155                 return wbsrv_samba3_sid2uid(s3call);
156
157         case WINBINDD_SID_TO_GID:
158         case WINBINDD_DUAL_SID2GID:
159                 return wbsrv_samba3_sid2gid(s3call);
160
161         case WINBINDD_UID_TO_SID:
162         case WINBINDD_DUAL_UID2SID:
163                 return wbsrv_samba3_uid2sid(s3call);
164
165         case WINBINDD_GID_TO_SID:
166         case WINBINDD_DUAL_GID2SID:
167                 return wbsrv_samba3_gid2sid(s3call);
168
169         case WINBINDD_DOMAIN_INFO:
170                 return wbsrv_samba3_domain_info(s3call);
171
172         case WINBINDD_PAM_LOGOFF:
173                 return wbsrv_samba3_pam_logoff(s3call);
174
175         /* Unimplemented commands */
176         case WINBINDD_GETPWSID:
177         case WINBINDD_PAM_CHAUTHTOK:
178         case WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP:
179         case WINBINDD_LOOKUPRIDS:
180         case WINBINDD_SIDS_TO_XIDS:
181         case WINBINDD_ALLOCATE_UID:
182         case WINBINDD_ALLOCATE_GID:
183         case WINBINDD_SET_MAPPING:
184         case WINBINDD_REMOVE_MAPPING:
185         case WINBINDD_SET_HWM:
186         case WINBINDD_SHOW_SEQUENCE:
187         case WINBINDD_WINS_BYIP:
188         case WINBINDD_WINS_BYNAME:
189         case WINBINDD_GETGRLST:
190         case WINBINDD_GETSIDALIASES:
191         case WINBINDD_DSGETDCNAME:
192         case WINBINDD_INIT_CONNECTION:
193         case WINBINDD_DUAL_SIDS2XIDS:
194         case WINBINDD_DUAL_SET_MAPPING:
195         case WINBINDD_DUAL_REMOVE_MAPPING:
196         case WINBINDD_DUAL_SET_HWM:
197         case WINBINDD_DUAL_USERINFO:
198         case WINBINDD_DUAL_GETSIDALIASES:
199         case WINBINDD_DUAL_NDRCMD:
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)
212 {
213         uint8_t *extra_data;
214         size_t extra_data_len = 0;
215
216         extra_data = (uint8_t *)call->response.extra_data.data;
217         if (extra_data != NULL) {
218                 extra_data_len = call->response.length -
219                         sizeof(call->response);
220         }
221
222         call->out = data_blob_talloc(call, NULL, call->response.length);
223         NT_STATUS_HAVE_NO_MEMORY(call->out.data);
224
225         /* don't push real pointer values into sockets */
226         if (extra_data) {
227                 call->response.extra_data.data = (void *)0xFFFFFFFF;
228         }
229         memcpy(call->out.data, &call->response, sizeof(call->response));
230         /* set back the pointer */
231         call->response.extra_data.data = extra_data;
232
233         if (extra_data) {
234                 memcpy(call->out.data + sizeof(call->response),
235                        extra_data,
236                        extra_data_len);
237         }
238
239         return NT_STATUS_OK;
240 }
241
242 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq);
243
244 /*
245  * queue a wbsrv_call reply on a wbsrv_connection
246  * NOTE: that this implies talloc_free(call),
247  *       use talloc_reference(call) if you need it after
248  *       calling wbsrv_queue_reply
249  */
250 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
251 {
252         struct wbsrv_connection *wbsrv_conn = call->wbconn;
253         struct tevent_req *subreq;
254         NTSTATUS status;
255
256         status = wbsrv_samba3_push_reply(call);
257         NT_STATUS_NOT_OK_RETURN(status);
258
259         call->out_iov[0].iov_base = call->out.data;
260         call->out_iov[0].iov_len = call->out.length;
261
262         subreq = tstream_writev_queue_send(call,
263                                            wbsrv_conn->conn->event.ctx,
264                                            wbsrv_conn->tstream,
265                                            wbsrv_conn->send_queue,
266                                            call->out_iov, 1);
267         if (subreq == NULL) {
268                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
269                                 "no memory for tstream_writev_queue_send");
270                 return NT_STATUS_NO_MEMORY;
271         }
272         tevent_req_set_callback(subreq, wbsrv_samba3_send_reply_done, call);
273
274         return status;
275 }
276
277 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq)
278 {
279         struct wbsrv_samba3_call *call = tevent_req_callback_data(subreq,
280                         struct wbsrv_samba3_call);
281         int sys_errno;
282         int rc;
283
284         rc = tstream_writev_queue_recv(subreq, &sys_errno);
285         TALLOC_FREE(subreq);
286         if (rc == -1) {
287                 const char *reason;
288
289                 reason = talloc_asprintf(call, "wbsrv_samba3_send_reply_done: "
290                                          "tstream_writev_queue_recv() - %d:%s",
291                                          sys_errno, strerror(sys_errno));
292                 if (reason == NULL) {
293                         reason = "wbsrv_samba3_send_reply_done: "
294                                  "tstream_writev_queue_recv() failed";
295                 }
296
297                 wbsrv_terminate_connection(call->wbconn, reason);
298                 return;
299         }
300
301         talloc_free(call);
302 }
303
304 NTSTATUS wbsrv_samba3_process(struct wbsrv_samba3_call *call)
305 {
306         NTSTATUS status;
307
308         status = wbsrv_samba3_pull_request(call);
309         
310         if (!NT_STATUS_IS_OK(status)) {
311                 return status;
312         }
313
314         status = wbsrv_samba3_handle_call(call);
315
316         if (!NT_STATUS_IS_OK(status)) {
317                 talloc_free(call);
318                 return status;
319         }
320
321         if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
322                 return NT_STATUS_OK;
323         }
324
325         status = wbsrv_samba3_send_reply(call);
326         return status;
327 }
328