f84f3048e97fdad9f1541025e5e17a4e8225500c
[kai/samba.git] / source3 / smbd / ipc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Inter-process communication and named pipe handling
5    Copyright (C) Andrew Tridgell 1992-1998
6
7    SMB Version handling
8    Copyright (C) John H Terpstra 1995-1998
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23    */
24 /*
25    This file handles the named pipe and mailslot calls
26    in the SMBtrans protocol
27    */
28
29 #include "includes.h"
30
31 extern int max_send;
32
33 extern fstring local_machine;
34
35 #define NERR_notsupported 50
36
37 extern int smb_read_error;
38
39 /*******************************************************************
40  copies parameters and data, as needed, into the smb buffer
41
42  *both* the data and params sections should be aligned.  this
43  is fudged in the rpc pipes by 
44  at present, only the data section is.  this may be a possible
45  cause of some of the ipc problems being experienced.  lkcl26dec97
46
47  ******************************************************************/
48
49 static void copy_trans_params_and_data(char *outbuf, int align,
50                                 char *rparam, int param_offset, int param_len,
51                                 char *rdata, int data_offset, int data_len)
52 {
53         char *copy_into = smb_buf(outbuf)+1;
54
55         if(param_len < 0)
56                 param_len = 0;
57
58         if(data_len < 0)
59                 data_len = 0;
60
61         DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d]\n",
62                         param_offset, param_offset + param_len,
63                         data_offset , data_offset  + data_len));
64
65         if (param_len)
66                 memcpy(copy_into, &rparam[param_offset], param_len);
67
68         copy_into += param_len + align;
69
70         if (data_len )
71                 memcpy(copy_into, &rdata[data_offset], data_len);
72 }
73
74 /****************************************************************************
75  Send a trans reply.
76  ****************************************************************************/
77
78 void send_trans_reply(char *outbuf,
79                                 char *rparam, int rparam_len,
80                                 char *rdata, int rdata_len,
81                                 BOOL buffer_too_large)
82 {
83         int this_ldata,this_lparam;
84         int tot_data_sent = 0;
85         int tot_param_sent = 0;
86         int align;
87
88         int ldata  = rdata  ? rdata_len : 0;
89         int lparam = rparam ? rparam_len : 0;
90
91         if (buffer_too_large)
92                 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
93
94         this_lparam = MIN(lparam,max_send - 500); /* hack */
95         this_ldata  = MIN(ldata,max_send - (500+this_lparam));
96
97         align = ((this_lparam)%4);
98
99         if (buffer_too_large) {
100                 ERROR_NT(STATUS_BUFFER_OVERFLOW);
101         }
102
103         set_message(outbuf,10,1+align+this_ldata+this_lparam,True);
104
105         copy_trans_params_and_data(outbuf, align,
106                                                                 rparam, tot_param_sent, this_lparam,
107                                                                 rdata, tot_data_sent, this_ldata);
108
109         SSVAL(outbuf,smb_vwv0,lparam);
110         SSVAL(outbuf,smb_vwv1,ldata);
111         SSVAL(outbuf,smb_vwv3,this_lparam);
112         SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
113         SSVAL(outbuf,smb_vwv5,0);
114         SSVAL(outbuf,smb_vwv6,this_ldata);
115         SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
116         SSVAL(outbuf,smb_vwv8,0);
117         SSVAL(outbuf,smb_vwv9,0);
118
119         show_msg(outbuf);
120         if (!send_smb(smbd_server_fd(),outbuf))
121                 exit_server("send_trans_reply: send_smb failed.");
122
123         tot_data_sent = this_ldata;
124         tot_param_sent = this_lparam;
125
126         while (tot_data_sent < ldata || tot_param_sent < lparam)
127         {
128                 this_lparam = MIN(lparam-tot_param_sent, max_send - 500); /* hack */
129                 this_ldata  = MIN(ldata -tot_data_sent, max_send - (500+this_lparam));
130
131                 if(this_lparam < 0)
132                         this_lparam = 0;
133
134                 if(this_ldata < 0)
135                         this_ldata = 0;
136
137                 align = (this_lparam%4);
138
139                 set_message(outbuf,10,1+this_ldata+this_lparam+align,False);
140
141                 copy_trans_params_and_data(outbuf, align,
142                                                                         rparam, tot_param_sent, this_lparam,
143                                                                         rdata, tot_data_sent, this_ldata);
144
145                 SSVAL(outbuf,smb_vwv3,this_lparam);
146                 SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
147                 SSVAL(outbuf,smb_vwv5,tot_param_sent);
148                 SSVAL(outbuf,smb_vwv6,this_ldata);
149                 SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
150                 SSVAL(outbuf,smb_vwv8,tot_data_sent);
151                 SSVAL(outbuf,smb_vwv9,0);
152
153                 show_msg(outbuf);
154                 if (!send_smb(smbd_server_fd(),outbuf))
155                         exit_server("send_trans_reply: send_smb failed.");
156
157                 tot_data_sent  += this_ldata;
158                 tot_param_sent += this_lparam;
159         }
160 }
161
162 /****************************************************************************
163  Start the first part of an RPC reply which began with an SMBtrans request.
164 ****************************************************************************/
165
166 static BOOL api_rpc_trans_reply(char *outbuf, smb_np_struct *p)
167 {
168         BOOL is_data_outstanding;
169         char *rdata = malloc(p->max_trans_reply);
170         int data_len;
171
172         if(rdata == NULL) {
173                 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
174                 return False;
175         }
176
177         if((data_len = read_from_pipe( p, rdata, p->max_trans_reply,
178                                         &is_data_outstanding)) < 0) {
179                 SAFE_FREE(rdata);
180                 return False;
181         }
182
183         send_trans_reply(outbuf, NULL, 0, rdata, data_len, is_data_outstanding);
184
185         SAFE_FREE(rdata);
186         return True;
187 }
188
189 /****************************************************************************
190  WaitNamedPipeHandleState 
191 ****************************************************************************/
192
193 static BOOL api_WNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len)
194 {
195         uint16 priority;
196
197         if (!param || param_len < 2)
198                 return False;
199
200         priority = SVAL(param,0);
201         DEBUG(4,("WaitNamedPipeHandleState priority %x\n", priority));
202
203         if (wait_rpc_pipe_hnd_state(p, priority)) {
204                 /* now send the reply */
205                 send_trans_reply(outbuf, NULL, 0, NULL, 0, False);
206                 return True;
207         }
208         return False;
209 }
210
211
212 /****************************************************************************
213  SetNamedPipeHandleState 
214 ****************************************************************************/
215
216 static BOOL api_SNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len)
217 {
218         uint16 id;
219
220         if (!param || param_len < 2)
221                 return False;
222
223         id = SVAL(param,0);
224         DEBUG(4,("SetNamedPipeHandleState to code %x\n", id));
225
226         if (set_rpc_pipe_hnd_state(p, id)) {
227                 /* now send the reply */
228                 send_trans_reply(outbuf, NULL, 0, NULL, 0, False);
229                 return True;
230         }
231         return False;
232 }
233
234
235 /****************************************************************************
236  When no reply is generated, indicate unsupported.
237  ****************************************************************************/
238
239 static BOOL api_no_reply(char *outbuf, int max_rdata_len)
240 {
241         char rparam[4];
242
243         /* unsupported */
244         SSVAL(rparam,0,NERR_notsupported);
245         SSVAL(rparam,2,0); /* converter word */
246
247         DEBUG(3,("Unsupported API fd command\n"));
248
249         /* now send the reply */
250         send_trans_reply(outbuf, rparam, 4, NULL, 0, False);
251
252         return -1;
253 }
254
255 /****************************************************************************
256  Handle remote api calls delivered to a named pipe already opened.
257  ****************************************************************************/
258
259 static int api_fd_reply(connection_struct *conn,uint16 vuid,char *outbuf,
260                         uint16 *setup,char *data,char *params,
261                         int suwcnt,int tdscnt,int tpscnt,int mdrcnt,int mprcnt)
262 {
263         BOOL reply = False;
264         smb_np_struct *p = NULL;
265         int pnum;
266         int subcommand;
267
268         DEBUG(5,("api_fd_reply\n"));
269
270         /* First find out the name of this file. */
271         if (suwcnt != 2) {
272                 DEBUG(0,("Unexpected named pipe transaction.\n"));
273                 return(-1);
274         }
275
276         /* Get the file handle and hence the file name. */
277         /* 
278          * NB. The setup array has already been transformed
279          * via SVAL and so is in gost byte order.
280          */
281         pnum = ((int)setup[1]) & 0xFFFF;
282         subcommand = ((int)setup[0]) & 0xFFFF;
283
284         if(!(p = get_rpc_pipe(pnum))) {
285                 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
286                 return api_no_reply(outbuf, mdrcnt);
287         }
288
289         DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)", subcommand, p->name, pnum));
290
291         /* record maximum data length that can be transmitted in an SMBtrans */
292         p->max_trans_reply = mdrcnt;
293
294         DEBUG(10,("api_fd_reply: p:%p max_trans_reply: %d\n", p, p->max_trans_reply));
295
296         switch (subcommand) {
297         case 0x26:
298                 /* dce/rpc command */
299                 reply = write_to_pipe(p, data, tdscnt);
300                 if (reply)
301                         reply = api_rpc_trans_reply(outbuf, p);
302                 break;
303         case 0x53:
304                 /* Wait Named Pipe Handle state */
305                 reply = api_WNPHS(outbuf, p, params, tpscnt);
306                 break;
307         case 0x01:
308                 /* Set Named Pipe Handle state */
309                 reply = api_SNPHS(outbuf, p, params, tpscnt);
310                 break;
311         }
312
313         if (!reply)
314                 return api_no_reply(outbuf, mdrcnt);
315
316         return -1;
317 }
318
319 /****************************************************************************
320   handle named pipe commands
321   ****************************************************************************/
322 static int named_pipe(connection_struct *conn,uint16 vuid, char *outbuf,char *name,
323                       uint16 *setup,char *data,char *params,
324                       int suwcnt,int tdscnt,int tpscnt,
325                       int msrcnt,int mdrcnt,int mprcnt)
326 {
327         DEBUG(3,("named pipe command on <%s> name\n", name));
328
329         if (strequal(name,"LANMAN"))
330                 return api_reply(conn,vuid,outbuf,data,params,tdscnt,tpscnt,mdrcnt,mprcnt);
331
332         if (strequal(name,"WKSSVC") ||
333             strequal(name,"SRVSVC") ||
334             strequal(name,"WINREG") ||
335             strequal(name,"SAMR") ||
336             strequal(name,"LSARPC"))
337         {
338                 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
339                 return api_fd_reply(conn,vuid,outbuf,setup,data,params,suwcnt,tdscnt,tpscnt,mdrcnt,mprcnt);
340         }
341
342         if (strlen(name) < 1)
343                 return api_fd_reply(conn,vuid,outbuf,setup,data,params,suwcnt,tdscnt,tpscnt,mdrcnt,mprcnt);
344
345         if (setup)
346                 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n", (int)setup[0],(int)setup[1]));
347
348         return 0;
349 }
350
351
352 /****************************************************************************
353  Reply to a SMBtrans.
354  ****************************************************************************/
355
356 int reply_trans(connection_struct *conn, char *inbuf,char *outbuf, int size, int bufsize)
357 {
358         fstring name;
359         int name_offset = 0;
360         char *data=NULL,*params=NULL;
361         uint16 *setup=NULL;
362         int outsize = 0;
363         uint16 vuid = SVAL(inbuf,smb_uid);
364         int tpscnt = SVAL(inbuf,smb_vwv0);
365         int tdscnt = SVAL(inbuf,smb_vwv1);
366         int mprcnt = SVAL(inbuf,smb_vwv2);
367         int mdrcnt = SVAL(inbuf,smb_vwv3);
368         int msrcnt = CVAL(inbuf,smb_vwv4);
369         BOOL close_on_completion = BITSETW(inbuf+smb_vwv5,0);
370         BOOL one_way = BITSETW(inbuf+smb_vwv5,1);
371         int pscnt = SVAL(inbuf,smb_vwv9);
372         int psoff = SVAL(inbuf,smb_vwv10);
373         int dscnt = SVAL(inbuf,smb_vwv11);
374         int dsoff = SVAL(inbuf,smb_vwv12);
375         int suwcnt = CVAL(inbuf,smb_vwv13);
376         START_PROFILE(SMBtrans);
377
378         memset(name, '\0',sizeof(name));
379         srvstr_pull(inbuf, name, smb_buf(inbuf), sizeof(name), -1, STR_TERMINATE);
380
381         if (dscnt > tdscnt || pscnt > tpscnt) {
382                 exit_server("invalid trans parameters");
383         }
384   
385         if (tdscnt)  {
386                 if((data = (char *)malloc(tdscnt)) == NULL) {
387                         DEBUG(0,("reply_trans: data malloc fail for %d bytes !\n", tdscnt));
388                         END_PROFILE(SMBtrans);
389                         return(ERROR_DOS(ERRDOS,ERRnomem));
390                 } 
391                 memcpy(data,smb_base(inbuf)+dsoff,dscnt);
392         }
393
394         if (tpscnt) {
395                 if((params = (char *)malloc(tpscnt)) == NULL) {
396                         DEBUG(0,("reply_trans: param malloc fail for %d bytes !\n", tpscnt));
397                         END_PROFILE(SMBtrans);
398                         return(ERROR_DOS(ERRDOS,ERRnomem));
399                 } 
400                 memcpy(params,smb_base(inbuf)+psoff,pscnt);
401         }
402
403         if (suwcnt) {
404                 int i;
405                 if((setup = (uint16 *)malloc(suwcnt*sizeof(uint16))) == NULL) {
406           DEBUG(0,("reply_trans: setup malloc fail for %d bytes !\n", (int)(suwcnt * sizeof(uint16))));
407                   END_PROFILE(SMBtrans);
408                   return(ERROR_DOS(ERRDOS,ERRnomem));
409         } 
410                 for (i=0;i<suwcnt;i++)
411                         setup[i] = SVAL(inbuf,smb_vwv14+i*SIZEOFWORD);
412         }
413
414
415         if (pscnt < tpscnt || dscnt < tdscnt) {
416                 /* We need to send an interim response then receive the rest
417                    of the parameter/data bytes */
418                 outsize = set_message(outbuf,0,0,True);
419                 show_msg(outbuf);
420                 if (!send_smb(smbd_server_fd(),outbuf))
421                         exit_server("reply_trans: send_smb failed.");
422         }
423
424         /* receive the rest of the trans packet */
425         while (pscnt < tpscnt || dscnt < tdscnt) {
426                 BOOL ret;
427                 int pcnt,poff,dcnt,doff,pdisp,ddisp;
428       
429                 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
430
431                 if ((ret && (CVAL(inbuf, smb_com) != SMBtranss)) || !ret) {
432                         if(ret) {
433                                 DEBUG(0,("reply_trans: Invalid secondary trans packet\n"));
434                         } else {
435                                 DEBUG(0,("reply_trans: %s in getting secondary trans response.\n",
436                                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
437                         }
438                         SAFE_FREE(params);
439                         SAFE_FREE(data);
440                         SAFE_FREE(setup);
441                         END_PROFILE(SMBtrans);
442                         return(ERROR_DOS(ERRSRV,ERRerror));
443                 }
444
445                 show_msg(inbuf);
446       
447                 tpscnt = SVAL(inbuf,smb_vwv0);
448                 tdscnt = SVAL(inbuf,smb_vwv1);
449
450                 pcnt = SVAL(inbuf,smb_vwv2);
451                 poff = SVAL(inbuf,smb_vwv3);
452                 pdisp = SVAL(inbuf,smb_vwv4);
453                 
454                 dcnt = SVAL(inbuf,smb_vwv5);
455                 doff = SVAL(inbuf,smb_vwv6);
456                 ddisp = SVAL(inbuf,smb_vwv7);
457                 
458                 pscnt += pcnt;
459                 dscnt += dcnt;
460                 
461                 if (dscnt > tdscnt || pscnt > tpscnt) {
462                         exit_server("invalid trans parameters");
463                 }
464                 
465                 if (pcnt)
466                         memcpy(params+pdisp,smb_base(inbuf)+poff,pcnt);
467                 if (dcnt)
468                         memcpy(data+ddisp,smb_base(inbuf)+doff,dcnt);      
469         }
470         
471         
472         DEBUG(3,("trans <%s> data=%d params=%d setup=%d\n",
473                  name,tdscnt,tpscnt,suwcnt));
474         
475         /*
476          * WinCE wierdness....
477          */
478
479         if (name[0] == '\\' && (StrnCaseCmp(&name[1],local_machine, strlen(local_machine)) == 0) &&
480                         (name[strlen(local_machine)+1] == '\\'))
481                 name_offset = strlen(local_machine)+1;
482
483         if (strnequal(&name[name_offset], "\\PIPE", strlen("\\PIPE"))) {
484                 name_offset += strlen("\\PIPE");
485
486                 /* Win9x weirdness.  When talking to a unicode server Win9x
487                    only sends \PIPE instead of \PIPE\ */
488
489                 if (name[name_offset] == '\\')
490                         name_offset++;
491
492                 DEBUG(5,("calling named_pipe\n"));
493                 outsize = named_pipe(conn,vuid,outbuf,
494                                      name+name_offset,setup,data,params,
495                                      suwcnt,tdscnt,tpscnt,msrcnt,mdrcnt,mprcnt);
496         } else {
497                 DEBUG(3,("invalid pipe name\n"));
498                 outsize = 0;
499         }
500
501         
502         SAFE_FREE(data);
503         SAFE_FREE(params);
504         SAFE_FREE(setup);
505         
506         if (close_on_completion)
507                 close_cnum(conn,vuid);
508
509         if (one_way) {
510                 END_PROFILE(SMBtrans);
511                 return(-1);
512         }
513         
514         if (outsize == 0) {
515                 END_PROFILE(SMBtrans);
516                 return(ERROR_DOS(ERRSRV,ERRnosupport));
517         }
518         
519         END_PROFILE(SMBtrans);
520         return(outsize);
521 }