Refactor the crypto code after a very helpful conversation
[samba.git] / source / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 extern int max_send;
24 extern enum protocol_types Protocol;
25 extern struct current_user current_user;
26
27 static const char *known_nt_pipes[] = {
28         "\\LANMAN",
29         "\\srvsvc",
30         "\\samr",
31         "\\wkssvc",
32         "\\NETLOGON",
33         "\\ntlsa",
34         "\\ntsvcs",
35         "\\lsass",
36         "\\lsarpc",
37         "\\winreg",
38         "\\initshutdown",
39         "\\spoolss",
40         "\\netdfs",
41         "\\rpcecho",
42         "\\svcctl",
43         "\\eventlog",
44         "\\unixinfo",
45         NULL
46 };
47
48 static char *nttrans_realloc(char **ptr, size_t size)
49 {
50         if (ptr==NULL) {
51                 smb_panic("nttrans_realloc() called with NULL ptr");
52         }
53
54         *ptr = (char *)SMB_REALLOC(*ptr, size);
55         if(*ptr == NULL) {
56                 return NULL;
57         }
58         memset(*ptr,'\0',size);
59         return *ptr;
60 }
61
62 /****************************************************************************
63  Send the required number of replies back.
64  We assume all fields other than the data fields are
65  set correctly for the type of call.
66  HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
68
69 void send_nt_replies(connection_struct *conn,
70                         struct smb_request *req, NTSTATUS nt_error,
71                      char *params, int paramsize,
72                      char *pdata, int datasize)
73 {
74         int data_to_send = datasize;
75         int params_to_send = paramsize;
76         int useable_space;
77         char *pp = params;
78         char *pd = pdata;
79         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80         int alignment_offset = 3;
81         int data_alignment_offset = 0;
82
83         /*
84          * If there genuinely are no parameters or data to send just send
85          * the empty packet.
86          */
87
88         if(params_to_send == 0 && data_to_send == 0) {
89                 reply_outbuf(req, 18, 0);
90                 show_msg((char *)req->outbuf);
91                 return;
92         }
93
94         /*
95          * When sending params and data ensure that both are nicely aligned.
96          * Only do this alignment when there is also data to send - else
97          * can cause NT redirector problems.
98          */
99
100         if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
101                 data_alignment_offset = 4 - (params_to_send % 4);
102         }
103
104         /*
105          * Space is bufsize minus Netbios over TCP header minus SMB header.
106          * The alignment_offset is to align the param bytes on a four byte
107          * boundary (2 bytes for data len, one byte pad).
108          * NT needs this to work correctly.
109          */
110
111         useable_space = max_send - (smb_size
112                                     + 2 * 18 /* wct */
113                                     + alignment_offset
114                                     + data_alignment_offset);
115
116         /*
117          * useable_space can never be more than max_send minus the
118          * alignment offset.
119          */
120
121         useable_space = MIN(useable_space,
122                                 max_send - (alignment_offset+data_alignment_offset));
123
124
125         while (params_to_send || data_to_send) {
126
127                 /*
128                  * Calculate whether we will totally or partially fill this packet.
129                  */
130
131                 total_sent_thistime = params_to_send + data_to_send +
132                                         alignment_offset + data_alignment_offset;
133
134                 /*
135                  * We can never send more than useable_space.
136                  */
137
138                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139
140                 reply_outbuf(req, 18, total_sent_thistime);
141
142                 /*
143                  * Set total params and data to be sent.
144                  */
145
146                 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
147                 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
148
149                 /*
150                  * Calculate how many parameters and data we can fit into
151                  * this packet. Parameters get precedence.
152                  */
153
154                 params_sent_thistime = MIN(params_to_send,useable_space);
155                 data_sent_thistime = useable_space - params_sent_thistime;
156                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157
158                 SIVAL(req->outbuf, smb_ntr_ParameterCount,
159                       params_sent_thistime);
160
161                 if(params_sent_thistime == 0) {
162                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
163                         SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
164                 } else {
165                         /*
166                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
167                          * parameter bytes, however the first 4 bytes of outbuf are
168                          * the Netbios over TCP header. Thus use smb_base() to subtract
169                          * them from the calculation.
170                          */
171
172                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,
173                               ((smb_buf(req->outbuf)+alignment_offset)
174                                - smb_base(req->outbuf)));
175                         /*
176                          * Absolute displacement of param bytes sent in this packet.
177                          */
178
179                         SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
180                               pp - params);
181                 }
182
183                 /*
184                  * Deal with the data portion.
185                  */
186
187                 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188
189                 if(data_sent_thistime == 0) {
190                         SIVAL(req->outbuf,smb_ntr_DataOffset,0);
191                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
192                 } else {
193                         /*
194                          * The offset of the data bytes is the offset of the
195                          * parameter bytes plus the number of parameters being sent this time.
196                          */
197
198                         SIVAL(req->outbuf, smb_ntr_DataOffset,
199                               ((smb_buf(req->outbuf)+alignment_offset) -
200                                smb_base(req->outbuf))
201                               + params_sent_thistime + data_alignment_offset);
202                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
203                 }
204
205                 /*
206                  * Copy the param bytes into the packet.
207                  */
208
209                 if(params_sent_thistime) {
210                         if (alignment_offset != 0) {
211                                 memset(smb_buf(req->outbuf), 0,
212                                        alignment_offset);
213                         }
214                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
215                                params_sent_thistime);
216                 }
217
218                 /*
219                  * Copy in the data bytes
220                  */
221
222                 if(data_sent_thistime) {
223                         if (data_alignment_offset != 0) {
224                                 memset((smb_buf(req->outbuf)+alignment_offset+
225                                         params_sent_thistime), 0,
226                                        data_alignment_offset);
227                         }
228                         memcpy(smb_buf(req->outbuf)+alignment_offset
229                                +params_sent_thistime+data_alignment_offset,
230                                pd,data_sent_thistime);
231                 }
232
233                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234                         params_sent_thistime, data_sent_thistime, useable_space));
235                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236                         params_to_send, data_to_send, paramsize, datasize));
237
238                 if (NT_STATUS_V(nt_error)) {
239                         error_packet_set((char *)req->outbuf,
240                                          0, 0, nt_error,
241                                          __LINE__,__FILE__);
242                 }
243
244                 /* Send the packet */
245                 show_msg((char *)req->outbuf);
246                 if (!srv_send_smb(smbd_server_fd(),
247                                 (char *)req->outbuf,
248                                 IS_CONN_ENCRYPTED(conn))) {
249                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
250                 }
251
252                 TALLOC_FREE(req->outbuf);
253
254                 pp += params_sent_thistime;
255                 pd += data_sent_thistime;
256
257                 params_to_send -= params_sent_thistime;
258                 data_to_send -= data_sent_thistime;
259
260                 /*
261                  * Sanity check
262                  */
263
264                 if(params_to_send < 0 || data_to_send < 0) {
265                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
266                                 params_to_send, data_to_send));
267                         return;
268                 }
269         }
270 }
271
272 /****************************************************************************
273  Is it an NTFS stream name ?
274 ****************************************************************************/
275
276 bool is_ntfs_stream_name(const char *fname)
277 {
278         if (lp_posix_pathnames()) {
279                 return False;
280         }
281         return (strchr_m(fname, ':') != NULL) ? True : False;
282 }
283
284 /****************************************************************************
285  Reply to an NT create and X call on a pipe
286 ****************************************************************************/
287
288 static void nt_open_pipe(char *fname, connection_struct *conn,
289                          struct smb_request *req, int *ppnum)
290 {
291         smb_np_struct *p = NULL;
292         int i;
293
294         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
295
296         /* See if it is one we want to handle. */
297
298         if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
299                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
300                                 ERRDOS, ERRbadpipe);
301                 return;
302         }
303
304         for( i = 0; known_nt_pipes[i]; i++ ) {
305                 if( strequal(fname,known_nt_pipes[i])) {
306                         break;
307                 }
308         }
309
310         if ( known_nt_pipes[i] == NULL ) {
311                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
312                                 ERRDOS, ERRbadpipe);
313                 return;
314         }
315
316         /* Strip \\ off the name. */
317         fname++;
318
319         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
320
321         p = open_rpc_pipe_p(fname, conn, req->vuid);
322         if (!p) {
323                 reply_doserror(req, ERRSRV, ERRnofids);
324                 return;
325         }
326
327         /* TODO: Add pipe to db */
328
329         if ( !store_pipe_opendb( p ) ) {
330                 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
331         }
332
333         *ppnum = p->pnum;
334         return;
335 }
336
337 /****************************************************************************
338  Reply to an NT create and X call for pipes.
339 ****************************************************************************/
340
341 static void do_ntcreate_pipe_open(connection_struct *conn,
342                                   struct smb_request *req)
343 {
344         char *fname = NULL;
345         int pnum = -1;
346         char *p = NULL;
347         uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
348         TALLOC_CTX *ctx = talloc_tos();
349
350         srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
351                         smb_buf(req->inbuf), STR_TERMINATE);
352
353         if (!fname) {
354                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
355                                 ERRDOS, ERRbadpipe);
356                 return;
357         }
358         nt_open_pipe(fname, conn, req, &pnum);
359
360         if (req->outbuf) {
361                 /* error reply */
362                 return;
363         }
364
365         /*
366          * Deal with pipe return.
367          */
368
369         if (flags & EXTENDED_RESPONSE_REQUIRED) {
370                 /* This is very strange. We
371                  * return 50 words, but only set
372                  * the wcnt to 42 ? It's definately
373                  * what happens on the wire....
374                  */
375                 reply_outbuf(req, 50, 0);
376                 SCVAL(req->outbuf,smb_wct,42);
377         } else {
378                 reply_outbuf(req, 34, 0);
379         }
380
381         p = (char *)req->outbuf + smb_vwv2;
382         p++;
383         SSVAL(p,0,pnum);
384         p += 2;
385         SIVAL(p,0,FILE_WAS_OPENED);
386         p += 4;
387         p += 32;
388         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
389         p += 20;
390         /* File type. */
391         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
392         /* Device state. */
393         SSVAL(p,2, 0x5FF); /* ? */
394         p += 4;
395
396         if (flags & EXTENDED_RESPONSE_REQUIRED) {
397                 p += 25;
398                 SIVAL(p,0,FILE_GENERIC_ALL);
399                 /*
400                  * For pipes W2K3 seems to return
401                  * 0x12019B next.
402                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
403                  */
404                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
405         }
406
407         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
408
409         chain_reply(req);
410 }
411
412 /****************************************************************************
413  Reply to an NT create and X call.
414 ****************************************************************************/
415
416 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
417 {
418         char *fname = NULL;
419         uint32 flags;
420         uint32 access_mask;
421         uint32 file_attributes;
422         uint32 share_access;
423         uint32 create_disposition;
424         uint32 create_options;
425         uint16 root_dir_fid;
426         SMB_BIG_UINT allocation_size;
427         /* Breakout the oplock request bits so we can set the
428            reply bits separately. */
429         uint32 fattr=0;
430         SMB_OFF_T file_len = 0;
431         SMB_STRUCT_STAT sbuf;
432         int info = 0;
433         files_struct *fsp = NULL;
434         char *p = NULL;
435         struct timespec c_timespec;
436         struct timespec a_timespec;
437         struct timespec m_timespec;
438         NTSTATUS status;
439         int oplock_request;
440         uint8_t oplock_granted = NO_OPLOCK_RETURN;
441         TALLOC_CTX *ctx = talloc_tos();
442
443         START_PROFILE(SMBntcreateX);
444
445         if (req->wct < 24) {
446                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
447                 return;
448         }
449
450         flags = IVAL(req->inbuf,smb_ntcreate_Flags);
451         access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
452         file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
453         share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
454         create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
455         create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
456         root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
457
458         allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
459                                              smb_ntcreate_AllocationSize);
460 #ifdef LARGE_SMB_OFF_T
461         allocation_size |= (((SMB_BIG_UINT)IVAL(
462                                      req->inbuf,
463                                      smb_ntcreate_AllocationSize + 4)) << 32);
464 #endif
465
466         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
467                         smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
468
469         if (!NT_STATUS_IS_OK(status)) {
470                 reply_nterror(req, status);
471                 END_PROFILE(SMBntcreateX);
472                 return;
473         }
474
475         DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
476                   "file_attributes = 0x%x, share_access = 0x%x, "
477                   "create_disposition = 0x%x create_options = 0x%x "
478                   "root_dir_fid = 0x%x, fname = %s\n",
479                         (unsigned int)flags,
480                         (unsigned int)access_mask,
481                         (unsigned int)file_attributes,
482                         (unsigned int)share_access,
483                         (unsigned int)create_disposition,
484                         (unsigned int)create_options,
485                         (unsigned int)root_dir_fid,
486                         fname));
487
488         /*
489          * If it's an IPC, use the pipe handler.
490          */
491
492         if (IS_IPC(conn)) {
493                 if (lp_nt_pipe_support()) {
494                         do_ntcreate_pipe_open(conn, req);
495                         END_PROFILE(SMBntcreateX);
496                         return;
497                 } else {
498                         reply_doserror(req, ERRDOS, ERRnoaccess);
499                         END_PROFILE(SMBntcreateX);
500                         return;
501                 }
502         }
503
504         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
505         if (oplock_request) {
506                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
507                         ? BATCH_OPLOCK : 0;
508         }
509
510         status = create_file(conn, req, root_dir_fid, fname,
511                              access_mask, share_access, create_disposition,
512                              create_options, file_attributes, oplock_request,
513                              allocation_size, NULL, NULL, &fsp, &info, &sbuf);
514
515         if (!NT_STATUS_IS_OK(status)) {
516                 if (open_was_deferred(req->mid)) {
517                         /* We have re-scheduled this call, no error. */
518                         END_PROFILE(SMBntcreateX);
519                         return;
520                 }
521                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
522                         reply_botherror(req, status, ERRDOS, ERRfilexists);
523                 }
524                 else {
525                         reply_nterror(req, status);
526                 }
527                 END_PROFILE(SMBntcreateX);
528                 return;
529         }
530
531         /*
532          * If the caller set the extended oplock request bit
533          * and we granted one (by whatever means) - set the
534          * correct bit for extended oplock reply.
535          */
536
537         if (oplock_request &&
538             (lp_fake_oplocks(SNUM(conn))
539              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
540
541                 /*
542                  * Exclusive oplock granted
543                  */
544
545                 if (flags & REQUEST_BATCH_OPLOCK) {
546                         oplock_granted = BATCH_OPLOCK_RETURN;
547                 } else {
548                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
549                 }
550         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
551                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
552         } else {
553                 oplock_granted = NO_OPLOCK_RETURN;
554         }
555
556         file_len = sbuf.st_size;
557         fattr = dos_mode(conn,fname,&sbuf);
558         if (fattr == 0) {
559                 fattr = FILE_ATTRIBUTE_NORMAL;
560         }
561
562         if (flags & EXTENDED_RESPONSE_REQUIRED) {
563                 /* This is very strange. We
564                  * return 50 words, but only set
565                  * the wcnt to 42 ? It's definately
566                  * what happens on the wire....
567                  */
568                 reply_outbuf(req, 50, 0);
569                 SCVAL(req->outbuf,smb_wct,42);
570         } else {
571                 reply_outbuf(req, 34, 0);
572         }
573
574         p = (char *)req->outbuf + smb_vwv2;
575
576         SCVAL(p, 0, oplock_granted);
577
578         p++;
579         SSVAL(p,0,fsp->fnum);
580         p += 2;
581         if ((create_disposition == FILE_SUPERSEDE)
582             && (info == FILE_WAS_OVERWRITTEN)) {
583                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
584         } else {
585                 SIVAL(p,0,info);
586         }
587         p += 4;
588
589         /* Create time. */
590         c_timespec = get_create_timespec(
591                 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
592         a_timespec = get_atimespec(&sbuf);
593         m_timespec = get_mtimespec(&sbuf);
594
595         if (lp_dos_filetime_resolution(SNUM(conn))) {
596                 dos_filetime_timespec(&c_timespec);
597                 dos_filetime_timespec(&a_timespec);
598                 dos_filetime_timespec(&m_timespec);
599         }
600
601         put_long_date_timespec(p, c_timespec); /* create time. */
602         p += 8;
603         put_long_date_timespec(p, a_timespec); /* access time */
604         p += 8;
605         put_long_date_timespec(p, m_timespec); /* write time */
606         p += 8;
607         put_long_date_timespec(p, m_timespec); /* change time */
608         p += 8;
609         SIVAL(p,0,fattr); /* File Attributes. */
610         p += 4;
611         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
612         p += 8;
613         SOFF_T(p,0,file_len);
614         p += 8;
615         if (flags & EXTENDED_RESPONSE_REQUIRED) {
616                 SSVAL(p,2,0x7);
617         }
618         p += 4;
619         SCVAL(p,0,fsp->is_directory ? 1 : 0);
620
621         if (flags & EXTENDED_RESPONSE_REQUIRED) {
622                 uint32 perms = 0;
623                 p += 25;
624                 if (fsp->is_directory
625                     || can_write_to_file(conn, fname, &sbuf)) {
626                         perms = FILE_GENERIC_ALL;
627                 } else {
628                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
629                 }
630                 SIVAL(p,0,perms);
631         }
632
633         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
634                  fsp->fnum, fsp->fsp_name));
635
636         chain_reply(req);
637         END_PROFILE(SMBntcreateX);
638         return;
639 }
640
641 /****************************************************************************
642  Reply to a NT_TRANSACT_CREATE call to open a pipe.
643 ****************************************************************************/
644
645 static void do_nt_transact_create_pipe(connection_struct *conn,
646                                        struct smb_request *req,
647                                        uint16 **ppsetup, uint32 setup_count,
648                                        char **ppparams, uint32 parameter_count,
649                                        char **ppdata, uint32 data_count)
650 {
651         char *fname = NULL;
652         char *params = *ppparams;
653         int pnum = -1;
654         char *p = NULL;
655         NTSTATUS status;
656         size_t param_len;
657         uint32 flags;
658         TALLOC_CTX *ctx = talloc_tos();
659
660         /*
661          * Ensure minimum number of parameters sent.
662          */
663
664         if(parameter_count < 54) {
665                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
666                 reply_doserror(req, ERRDOS, ERRnoaccess);
667                 return;
668         }
669
670         flags = IVAL(params,0);
671
672         srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
673                         parameter_count-53, STR_TERMINATE,
674                         &status);
675         if (!NT_STATUS_IS_OK(status)) {
676                 reply_nterror(req, status);
677                 return;
678         }
679
680         nt_open_pipe(fname, conn, req, &pnum);
681
682         if (req->outbuf) {
683                 /* Error return */
684                 return;
685         }
686
687         /* Realloc the size of parameters and data we will return */
688         if (flags & EXTENDED_RESPONSE_REQUIRED) {
689                 /* Extended response is 32 more byyes. */
690                 param_len = 101;
691         } else {
692                 param_len = 69;
693         }
694         params = nttrans_realloc(ppparams, param_len);
695         if(params == NULL) {
696                 reply_doserror(req, ERRDOS, ERRnomem);
697                 return;
698         }
699
700         p = params;
701         SCVAL(p,0,NO_OPLOCK_RETURN);
702
703         p += 2;
704         SSVAL(p,0,pnum);
705         p += 2;
706         SIVAL(p,0,FILE_WAS_OPENED);
707         p += 8;
708
709         p += 32;
710         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
711         p += 20;
712         /* File type. */
713         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
714         /* Device state. */
715         SSVAL(p,2, 0x5FF); /* ? */
716         p += 4;
717
718         if (flags & EXTENDED_RESPONSE_REQUIRED) {
719                 p += 25;
720                 SIVAL(p,0,FILE_GENERIC_ALL);
721                 /*
722                  * For pipes W2K3 seems to return
723                  * 0x12019B next.
724                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
725                  */
726                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
727         }
728
729         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
730
731         /* Send the required number of replies */
732         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
733
734         return;
735 }
736
737 /****************************************************************************
738  Internal fn to set security descriptors.
739 ****************************************************************************/
740
741 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
742                        uint32 security_info_sent)
743 {
744         SEC_DESC *psd = NULL;
745         NTSTATUS status;
746
747         if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
748                 return NT_STATUS_OK;
749         }
750
751         status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
752
753         if (!NT_STATUS_IS_OK(status)) {
754                 return status;
755         }
756
757         if (psd->owner_sid==0) {
758                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
759         }
760         if (psd->group_sid==0) {
761                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
762         }
763         if (psd->sacl==0) {
764                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
765         }
766         if (psd->dacl==0) {
767                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
768         }
769
770         if (fsp->fh->fd != -1) {
771                 status = SMB_VFS_FSET_NT_ACL(fsp, fsp->fh->fd,
772                                              security_info_sent, psd);
773         }
774         else {
775                 status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name,
776                                             security_info_sent, psd);
777         }
778
779         TALLOC_FREE(psd);
780
781         return status;
782 }
783
784 /****************************************************************************
785  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
786 ****************************************************************************/
787
788 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
789 {
790         struct ea_list *ea_list_head = NULL;
791         size_t offset = 0;
792
793         if (data_size < 4) {
794                 return NULL;
795         }
796
797         while (offset + 4 <= data_size) {
798                 size_t next_offset = IVAL(pdata,offset);
799                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
800
801                 if (!eal) {
802                         return NULL;
803                 }
804
805                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
806                 if (next_offset == 0) {
807                         break;
808                 }
809                 offset += next_offset;
810         }
811
812         return ea_list_head;
813 }
814
815 /****************************************************************************
816  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
817 ****************************************************************************/
818
819 static void call_nt_transact_create(connection_struct *conn,
820                                     struct smb_request *req,
821                                     uint16 **ppsetup, uint32 setup_count,
822                                     char **ppparams, uint32 parameter_count,
823                                     char **ppdata, uint32 data_count,
824                                     uint32 max_data_count)
825 {
826         char *fname = NULL;
827         char *params = *ppparams;
828         char *data = *ppdata;
829         /* Breakout the oplock request bits so we can set the reply bits separately. */
830         uint32 fattr=0;
831         SMB_OFF_T file_len = 0;
832         SMB_STRUCT_STAT sbuf;
833         int info = 0;
834         files_struct *fsp = NULL;
835         char *p = NULL;
836         uint32 flags;
837         uint32 access_mask;
838         uint32 file_attributes;
839         uint32 share_access;
840         uint32 create_disposition;
841         uint32 create_options;
842         uint32 sd_len;
843         struct security_descriptor *sd = NULL;
844         uint32 ea_len;
845         uint16 root_dir_fid;
846         struct timespec c_timespec;
847         struct timespec a_timespec;
848         struct timespec m_timespec;
849         struct ea_list *ea_list = NULL;
850         NTSTATUS status;
851         size_t param_len;
852         SMB_BIG_UINT allocation_size;
853         int oplock_request;
854         uint8_t oplock_granted;
855         TALLOC_CTX *ctx = talloc_tos();
856
857         DEBUG(5,("call_nt_transact_create\n"));
858
859         /*
860          * If it's an IPC, use the pipe handler.
861          */
862
863         if (IS_IPC(conn)) {
864                 if (lp_nt_pipe_support()) {
865                         do_nt_transact_create_pipe(
866                                 conn, req,
867                                 ppsetup, setup_count,
868                                 ppparams, parameter_count,
869                                 ppdata, data_count);
870                         return;
871                 } else {
872                         reply_doserror(req, ERRDOS, ERRnoaccess);
873                         return;
874                 }
875         }
876
877         /*
878          * Ensure minimum number of parameters sent.
879          */
880
881         if(parameter_count < 54) {
882                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
883                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
884                 return;
885         }
886
887         flags = IVAL(params,0);
888         access_mask = IVAL(params,8);
889         file_attributes = IVAL(params,20);
890         share_access = IVAL(params,24);
891         create_disposition = IVAL(params,28);
892         create_options = IVAL(params,32);
893         sd_len = IVAL(params,36);
894         ea_len = IVAL(params,40);
895         root_dir_fid = (uint16)IVAL(params,4);
896         allocation_size = (SMB_BIG_UINT)IVAL(params,12);
897 #ifdef LARGE_SMB_OFF_T
898         allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
899 #endif
900
901         /* Ensure the data_len is correct for the sd and ea values given. */
902         if ((ea_len + sd_len > data_count)
903             || (ea_len > data_count) || (sd_len > data_count)
904             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
905                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
906                            "%u, data_count = %u\n", (unsigned int)ea_len,
907                            (unsigned int)sd_len, (unsigned int)data_count));
908                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
909                 return;
910         }
911
912         if (sd_len) {
913                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
914                            sd_len));
915
916                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
917                                              &sd);
918                 if (!NT_STATUS_IS_OK(status)) {
919                         DEBUG(10, ("call_nt_transact_create: "
920                                    "unmarshall_sec_desc failed: %s\n",
921                                    nt_errstr(status)));
922                         reply_nterror(req, status);
923                         return;
924                 }
925         }
926
927         if (ea_len) {
928                 if (!lp_ea_support(SNUM(conn))) {
929                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
930                                    "EA's not supported.\n",
931                                    (unsigned int)ea_len));
932                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
933                         return;
934                 }
935
936                 if (ea_len < 10) {
937                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
938                                   "too small (should be more than 10)\n",
939                                   (unsigned int)ea_len ));
940                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
941                         return;
942                 }
943
944                 /* We have already checked that ea_len <= data_count here. */
945                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
946                                                ea_len);
947                 if (ea_list == NULL) {
948                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
949                         return;
950                 }
951         }
952
953         srvstr_get_path(ctx, params, req->flags2, &fname,
954                         params+53, parameter_count-53,
955                         STR_TERMINATE, &status);
956         if (!NT_STATUS_IS_OK(status)) {
957                 reply_nterror(req, status);
958                 return;
959         }
960
961         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
962         if (oplock_request) {
963                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
964                         ? BATCH_OPLOCK : 0;
965         }
966
967         status = create_file(conn, req, root_dir_fid, fname,
968                              access_mask, share_access, create_disposition,
969                              create_options, file_attributes, oplock_request,
970                              allocation_size, sd, ea_list, &fsp, &info, &sbuf);
971
972         if(!NT_STATUS_IS_OK(status)) {
973                 if (open_was_deferred(req->mid)) {
974                         /* We have re-scheduled this call, no error. */
975                         return;
976                 }
977                 reply_openerror(req, status);
978                 return;
979         }
980
981         /*
982          * If the caller set the extended oplock request bit
983          * and we granted one (by whatever means) - set the
984          * correct bit for extended oplock reply.
985          */
986
987         if (oplock_request &&
988             (lp_fake_oplocks(SNUM(conn))
989              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
990
991                 /*
992                  * Exclusive oplock granted
993                  */
994
995                 if (flags & REQUEST_BATCH_OPLOCK) {
996                         oplock_granted = BATCH_OPLOCK_RETURN;
997                 } else {
998                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
999                 }
1000         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1001                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1002         } else {
1003                 oplock_granted = NO_OPLOCK_RETURN;
1004         }
1005
1006         file_len = sbuf.st_size;
1007         fattr = dos_mode(conn,fname,&sbuf);
1008         if (fattr == 0) {
1009                 fattr = FILE_ATTRIBUTE_NORMAL;
1010         }
1011
1012         /* Realloc the size of parameters and data we will return */
1013         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1014                 /* Extended response is 32 more byyes. */
1015                 param_len = 101;
1016         } else {
1017                 param_len = 69;
1018         }
1019         params = nttrans_realloc(ppparams, param_len);
1020         if(params == NULL) {
1021                 reply_doserror(req, ERRDOS, ERRnomem);
1022                 return;
1023         }
1024
1025         p = params;
1026         SCVAL(p, 0, oplock_granted);
1027
1028         p += 2;
1029         SSVAL(p,0,fsp->fnum);
1030         p += 2;
1031         if ((create_disposition == FILE_SUPERSEDE)
1032             && (info == FILE_WAS_OVERWRITTEN)) {
1033                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1034         } else {
1035                 SIVAL(p,0,info);
1036         }
1037         p += 8;
1038
1039         /* Create time. */
1040         c_timespec = get_create_timespec(
1041                 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1042         a_timespec = get_atimespec(&sbuf);
1043         m_timespec = get_mtimespec(&sbuf);
1044
1045         if (lp_dos_filetime_resolution(SNUM(conn))) {
1046                 dos_filetime_timespec(&c_timespec);
1047                 dos_filetime_timespec(&a_timespec);
1048                 dos_filetime_timespec(&m_timespec);
1049         }
1050
1051         put_long_date_timespec(p, c_timespec); /* create time. */
1052         p += 8;
1053         put_long_date_timespec(p, a_timespec); /* access time */
1054         p += 8;
1055         put_long_date_timespec(p, m_timespec); /* write time */
1056         p += 8;
1057         put_long_date_timespec(p, m_timespec); /* change time */
1058         p += 8;
1059         SIVAL(p,0,fattr); /* File Attributes. */
1060         p += 4;
1061         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1062         p += 8;
1063         SOFF_T(p,0,file_len);
1064         p += 8;
1065         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1066                 SSVAL(p,2,0x7);
1067         }
1068         p += 4;
1069         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1070
1071         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1072                 uint32 perms = 0;
1073                 p += 25;
1074                 if (fsp->is_directory
1075                     || can_write_to_file(conn, fname, &sbuf)) {
1076                         perms = FILE_GENERIC_ALL;
1077                 } else {
1078                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1079                 }
1080                 SIVAL(p,0,perms);
1081         }
1082
1083         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1084
1085         /* Send the required number of replies */
1086         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1087
1088         return;
1089 }
1090
1091 /****************************************************************************
1092  Reply to a NT CANCEL request.
1093  conn POINTER CAN BE NULL HERE !
1094 ****************************************************************************/
1095
1096 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1097 {
1098         /*
1099          * Go through and cancel any pending change notifies.
1100          */
1101
1102         START_PROFILE(SMBntcancel);
1103         remove_pending_change_notify_requests_by_mid(req->mid);
1104         remove_pending_lock_requests_by_mid(req->mid);
1105         srv_cancel_sign_response(req->mid);
1106
1107         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1108
1109         END_PROFILE(SMBntcancel);
1110         return;
1111 }
1112
1113 /****************************************************************************
1114  Copy a file.
1115 ****************************************************************************/
1116
1117 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1118                                 connection_struct *conn,
1119                                 struct smb_request *req,
1120                                 const char *oldname_in,
1121                                 const char *newname_in,
1122                                 uint32 attrs)
1123 {
1124         SMB_STRUCT_STAT sbuf1, sbuf2;
1125         char *oldname = NULL;
1126         char *newname = NULL;
1127         char *last_component_oldname = NULL;
1128         char *last_component_newname = NULL;
1129         files_struct *fsp1,*fsp2;
1130         uint32 fattr;
1131         int info;
1132         SMB_OFF_T ret=-1;
1133         NTSTATUS status = NT_STATUS_OK;
1134
1135         ZERO_STRUCT(sbuf1);
1136         ZERO_STRUCT(sbuf2);
1137
1138         if (!CAN_WRITE(conn)) {
1139                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1140         }
1141
1142         status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1143                         &last_component_oldname, &sbuf1);
1144         if (!NT_STATUS_IS_OK(status)) {
1145                 return status;
1146         }
1147
1148         status = check_name(conn, oldname);
1149         if (!NT_STATUS_IS_OK(status)) {
1150                 return status;
1151         }
1152
1153         /* Source must already exist. */
1154         if (!VALID_STAT(sbuf1)) {
1155                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1156         }
1157         /* Ensure attributes match. */
1158         fattr = dos_mode(conn,oldname,&sbuf1);
1159         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1160                 return NT_STATUS_NO_SUCH_FILE;
1161         }
1162
1163         status = unix_convert(ctx, conn, newname_in, False, &newname,
1164                         &last_component_newname, &sbuf2);
1165         if (!NT_STATUS_IS_OK(status)) {
1166                 return status;
1167         }
1168
1169         status = check_name(conn, newname);
1170         if (!NT_STATUS_IS_OK(status)) {
1171                 return status;
1172         }
1173
1174         /* Disallow if newname already exists. */
1175         if (VALID_STAT(sbuf2)) {
1176                 return NT_STATUS_OBJECT_NAME_COLLISION;
1177         }
1178
1179         /* No links from a directory. */
1180         if (S_ISDIR(sbuf1.st_mode)) {
1181                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1182         }
1183
1184         /* Ensure this is within the share. */
1185         status = check_reduced_name(conn, oldname);
1186         if (!NT_STATUS_IS_OK(status)) {
1187                 return status;
1188         }
1189
1190         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1191                                 oldname, newname));
1192
1193         status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1194                         FILE_READ_DATA, /* Read-only. */
1195                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1196                         FILE_OPEN,
1197                         0, /* No create options. */
1198                         FILE_ATTRIBUTE_NORMAL,
1199                         NO_OPLOCK,
1200                         &info, &fsp1);
1201
1202         if (!NT_STATUS_IS_OK(status)) {
1203                 return status;
1204         }
1205
1206         status = open_file_ntcreate(conn, req, newname, &sbuf2,
1207                         FILE_WRITE_DATA, /* Read-only. */
1208                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1209                         FILE_CREATE,
1210                         0, /* No create options. */
1211                         fattr,
1212                         NO_OPLOCK,
1213                         &info, &fsp2);
1214
1215         if (!NT_STATUS_IS_OK(status)) {
1216                 close_file(fsp1,ERROR_CLOSE);
1217                 return status;
1218         }
1219
1220         if (sbuf1.st_size) {
1221                 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1222         }
1223
1224         /*
1225          * As we are opening fsp1 read-only we only expect
1226          * an error on close on fsp2 if we are out of space.
1227          * Thus we don't look at the error return from the
1228          * close of fsp1.
1229          */
1230         close_file(fsp1,NORMAL_CLOSE);
1231
1232         /* Ensure the modtime is set correctly on the destination file. */
1233         fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1234
1235         status = close_file(fsp2,NORMAL_CLOSE);
1236
1237         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1238            creates the file. This isn't the correct thing to do in the copy
1239            case. JRA */
1240         file_set_dosmode(conn, newname, fattr, &sbuf2,
1241                          parent_dirname(newname),false);
1242
1243         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1244                 return NT_STATUS_DISK_FULL;
1245         }
1246
1247         if (!NT_STATUS_IS_OK(status)) {
1248                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1249                         nt_errstr(status), oldname, newname));
1250         }
1251         return status;
1252 }
1253
1254 /****************************************************************************
1255  Reply to a NT rename request.
1256 ****************************************************************************/
1257
1258 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1259 {
1260         char *oldname = NULL;
1261         char *newname = NULL;
1262         char *p;
1263         NTSTATUS status;
1264         bool src_has_wcard = False;
1265         bool dest_has_wcard = False;
1266         uint32 attrs;
1267         uint16 rename_type;
1268         TALLOC_CTX *ctx = talloc_tos();
1269
1270         START_PROFILE(SMBntrename);
1271
1272         if (req->wct < 4) {
1273                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1274                 END_PROFILE(SMBntrename);
1275                 return;
1276         }
1277
1278         attrs = SVAL(req->inbuf,smb_vwv0);
1279         rename_type = SVAL(req->inbuf,smb_vwv1);
1280
1281         p = smb_buf(req->inbuf) + 1;
1282         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1283                                    0, STR_TERMINATE, &status,
1284                                    &src_has_wcard);
1285         if (!NT_STATUS_IS_OK(status)) {
1286                 reply_nterror(req, status);
1287                 END_PROFILE(SMBntrename);
1288                 return;
1289         }
1290
1291         if( is_ntfs_stream_name(oldname)) {
1292                 /* Can't rename a stream. */
1293                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1294                 END_PROFILE(SMBntrename);
1295                 return;
1296         }
1297
1298         if (ms_has_wild(oldname)) {
1299                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1300                 END_PROFILE(SMBntrename);
1301                 return;
1302         }
1303
1304         p++;
1305         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1306                                    0, STR_TERMINATE, &status,
1307                                    &dest_has_wcard);
1308         if (!NT_STATUS_IS_OK(status)) {
1309                 reply_nterror(req, status);
1310                 END_PROFILE(SMBntrename);
1311                 return;
1312         }
1313
1314         status = resolve_dfspath(ctx, conn,
1315                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1316                                 oldname,
1317                                 &oldname);
1318         if (!NT_STATUS_IS_OK(status)) {
1319                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1320                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1321                                         ERRSRV, ERRbadpath);
1322                         END_PROFILE(SMBntrename);
1323                         return;
1324                 }
1325                 reply_nterror(req, status);
1326                 END_PROFILE(SMBntrename);
1327                 return;
1328         }
1329
1330         status = resolve_dfspath(ctx, conn,
1331                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1332                                 newname,
1333                                 &newname);
1334         if (!NT_STATUS_IS_OK(status)) {
1335                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1336                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1337                                         ERRSRV, ERRbadpath);
1338                         END_PROFILE(SMBntrename);
1339                         return;
1340                 }
1341                 reply_nterror(req, status);
1342                 END_PROFILE(SMBntrename);
1343                 return;
1344         }
1345
1346         DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1347
1348         switch(rename_type) {
1349                 case RENAME_FLAG_RENAME:
1350                         status = rename_internals(ctx, conn, req, oldname,
1351                                         newname, attrs, False, src_has_wcard,
1352                                         dest_has_wcard);
1353                         break;
1354                 case RENAME_FLAG_HARD_LINK:
1355                         if (src_has_wcard || dest_has_wcard) {
1356                                 /* No wildcards. */
1357                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1358                         } else {
1359                                 status = hardlink_internals(ctx,
1360                                                 conn,
1361                                                 oldname,
1362                                                 newname);
1363                         }
1364                         break;
1365                 case RENAME_FLAG_COPY:
1366                         if (src_has_wcard || dest_has_wcard) {
1367                                 /* No wildcards. */
1368                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1369                         } else {
1370                                 status = copy_internals(ctx, conn, req, oldname,
1371                                                         newname, attrs);
1372                         }
1373                         break;
1374                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1375                         status = NT_STATUS_INVALID_PARAMETER;
1376                         break;
1377                 default:
1378                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1379                         break;
1380         }
1381
1382         if (!NT_STATUS_IS_OK(status)) {
1383                 if (open_was_deferred(req->mid)) {
1384                         /* We have re-scheduled this call. */
1385                         END_PROFILE(SMBntrename);
1386                         return;
1387                 }
1388
1389                 reply_nterror(req, status);
1390                 END_PROFILE(SMBntrename);
1391                 return;
1392         }
1393
1394         reply_outbuf(req, 0, 0);
1395
1396         END_PROFILE(SMBntrename);
1397         return;
1398 }
1399
1400 /****************************************************************************
1401  Reply to a notify change - queue the request and
1402  don't allow a directory to be opened.
1403 ****************************************************************************/
1404
1405 static void call_nt_transact_notify_change(connection_struct *conn,
1406                                            struct smb_request *req,
1407                                            uint16 **ppsetup,
1408                                            uint32 setup_count,
1409                                            char **ppparams,
1410                                            uint32 parameter_count,
1411                                            char **ppdata, uint32 data_count,
1412                                            uint32 max_data_count,
1413                                            uint32 max_param_count)
1414 {
1415         uint16 *setup = *ppsetup;
1416         files_struct *fsp;
1417         uint32 filter;
1418         NTSTATUS status;
1419         bool recursive;
1420
1421         if(setup_count < 6) {
1422                 reply_doserror(req, ERRDOS, ERRbadfunc);
1423                 return;
1424         }
1425
1426         fsp = file_fsp(SVAL(setup,4));
1427         filter = IVAL(setup, 0);
1428         recursive = (SVAL(setup, 6) != 0) ? True : False;
1429
1430         DEBUG(3,("call_nt_transact_notify_change\n"));
1431
1432         if(!fsp) {
1433                 reply_doserror(req, ERRDOS, ERRbadfid);
1434                 return;
1435         }
1436
1437         {
1438                 char *filter_string;
1439
1440                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1441                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1442                         return;
1443                 }
1444
1445                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1446                          "called on %s, filter = %s, recursive = %d\n",
1447                          fsp->fsp_name, filter_string, recursive));
1448
1449                 TALLOC_FREE(filter_string);
1450         }
1451
1452         if((!fsp->is_directory) || (conn != fsp->conn)) {
1453                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1454                 return;
1455         }
1456
1457         if (fsp->notify == NULL) {
1458
1459                 status = change_notify_create(fsp, filter, recursive);
1460
1461                 if (!NT_STATUS_IS_OK(status)) {
1462                         DEBUG(10, ("change_notify_create returned %s\n",
1463                                    nt_errstr(status)));
1464                         reply_nterror(req, status);
1465                         return;
1466                 }
1467         }
1468
1469         if (fsp->notify->num_changes != 0) {
1470
1471                 /*
1472                  * We've got changes pending, respond immediately
1473                  */
1474
1475                 /*
1476                  * TODO: write a torture test to check the filtering behaviour
1477                  * here.
1478                  */
1479
1480                 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1481
1482                 /*
1483                  * change_notify_reply() above has independently sent its
1484                  * results
1485                  */
1486                 return;
1487         }
1488
1489         /*
1490          * No changes pending, queue the request
1491          */
1492
1493         status = change_notify_add_request(req,
1494                         max_param_count,
1495                         filter,
1496                         recursive, fsp);
1497         if (!NT_STATUS_IS_OK(status)) {
1498                 reply_nterror(req, status);
1499         }
1500         return;
1501 }
1502
1503 /****************************************************************************
1504  Reply to an NT transact rename command.
1505 ****************************************************************************/
1506
1507 static void call_nt_transact_rename(connection_struct *conn,
1508                                     struct smb_request *req,
1509                                     uint16 **ppsetup, uint32 setup_count,
1510                                     char **ppparams, uint32 parameter_count,
1511                                     char **ppdata, uint32 data_count,
1512                                     uint32 max_data_count)
1513 {
1514         char *params = *ppparams;
1515         char *new_name = NULL;
1516         files_struct *fsp = NULL;
1517         bool replace_if_exists = False;
1518         bool dest_has_wcard = False;
1519         NTSTATUS status;
1520         TALLOC_CTX *ctx = talloc_tos();
1521
1522         if(parameter_count < 5) {
1523                 reply_doserror(req, ERRDOS, ERRbadfunc);
1524                 return;
1525         }
1526
1527         fsp = file_fsp(SVAL(params, 0));
1528         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1529         if (!check_fsp(conn, req, fsp, &current_user)) {
1530                 return;
1531         }
1532         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1533                               parameter_count - 4,
1534                               STR_TERMINATE, &status, &dest_has_wcard);
1535         if (!NT_STATUS_IS_OK(status)) {
1536                 reply_nterror(req, status);
1537                 return;
1538         }
1539
1540         status = rename_internals(ctx,
1541                         conn,
1542                         req,
1543                         fsp->fsp_name,
1544                         new_name,
1545                         0,
1546                         replace_if_exists,
1547                         False,
1548                         dest_has_wcard);
1549
1550         if (!NT_STATUS_IS_OK(status)) {
1551                 if (open_was_deferred(req->mid)) {
1552                         /* We have re-scheduled this call. */
1553                         return;
1554                 }
1555                 reply_nterror(req, status);
1556                 return;
1557         }
1558
1559         /*
1560          * Rename was successful.
1561          */
1562         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1563
1564         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1565                  fsp->fsp_name, new_name));
1566
1567         return;
1568 }
1569
1570 /******************************************************************************
1571  Fake up a completely empty SD.
1572 *******************************************************************************/
1573
1574 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1575 {
1576         size_t sd_size;
1577
1578         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1579         if(!*ppsd) {
1580                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1581                 return NT_STATUS_NO_MEMORY;
1582         }
1583
1584         return NT_STATUS_OK;
1585 }
1586
1587 /****************************************************************************
1588  Reply to query a security descriptor.
1589 ****************************************************************************/
1590
1591 static void call_nt_transact_query_security_desc(connection_struct *conn,
1592                                                  struct smb_request *req,
1593                                                  uint16 **ppsetup,
1594                                                  uint32 setup_count,
1595                                                  char **ppparams,
1596                                                  uint32 parameter_count,
1597                                                  char **ppdata,
1598                                                  uint32 data_count,
1599                                                  uint32 max_data_count)
1600 {
1601         char *params = *ppparams;
1602         char *data = *ppdata;
1603         SEC_DESC *psd = NULL;
1604         size_t sd_size;
1605         uint32 security_info_wanted;
1606         TALLOC_CTX *frame;
1607         files_struct *fsp = NULL;
1608         NTSTATUS status;
1609         DATA_BLOB blob;
1610
1611         if(parameter_count < 8) {
1612                 reply_doserror(req, ERRDOS, ERRbadfunc);
1613                 return;
1614         }
1615
1616         fsp = file_fsp(SVAL(params,0));
1617         if(!fsp) {
1618                 reply_doserror(req, ERRDOS, ERRbadfid);
1619                 return;
1620         }
1621
1622         security_info_wanted = IVAL(params,4);
1623
1624         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1625                         (unsigned int)security_info_wanted ));
1626
1627         params = nttrans_realloc(ppparams, 4);
1628         if(params == NULL) {
1629                 reply_doserror(req, ERRDOS, ERRnomem);
1630                 return;
1631         }
1632
1633         frame = talloc_stackframe();
1634
1635         /*
1636          * Get the permissions to return.
1637          */
1638
1639         if (!lp_nt_acl_support(SNUM(conn))) {
1640                 status = get_null_nt_acl(talloc_tos(), &psd);
1641         } else {
1642                 if (fsp->fh->fd != -1) {
1643                         status = SMB_VFS_FGET_NT_ACL(
1644                                 fsp, fsp->fh->fd, security_info_wanted, &psd);
1645                 }
1646                 else {
1647                         status = SMB_VFS_GET_NT_ACL(
1648                                 conn, fsp->fsp_name, security_info_wanted, &psd);
1649                 }
1650         }
1651
1652         if (!NT_STATUS_IS_OK(status)) {
1653                 TALLOC_FREE(frame);
1654                 reply_nterror(req, status);
1655                 return;
1656         }
1657
1658         sd_size = ndr_size_security_descriptor(psd, 0);
1659
1660         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1661
1662         SIVAL(params,0,(uint32)sd_size);
1663
1664         if (max_data_count < sd_size) {
1665                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1666                                 params, 4, *ppdata, 0);
1667                 TALLOC_FREE(frame);
1668                 return;
1669         }
1670
1671         /*
1672          * Allocate the data we will point this at.
1673          */
1674
1675         data = nttrans_realloc(ppdata, sd_size);
1676         if(data == NULL) {
1677                 TALLOC_FREE(frame);
1678                 reply_doserror(req, ERRDOS, ERRnomem);
1679                 return;
1680         }
1681
1682         status = marshall_sec_desc(talloc_tos(), psd,
1683                                    &blob.data, &blob.length);
1684
1685         if (!NT_STATUS_IS_OK(status)) {
1686                 TALLOC_FREE(frame);
1687                 reply_nterror(req, status);
1688                 return;
1689         }
1690
1691         SMB_ASSERT(sd_size == blob.length);
1692         memcpy(data, blob.data, sd_size);
1693
1694         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1695
1696         TALLOC_FREE(frame);
1697         return;
1698 }
1699
1700 /****************************************************************************
1701  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1702 ****************************************************************************/
1703
1704 static void call_nt_transact_set_security_desc(connection_struct *conn,
1705                                                struct smb_request *req,
1706                                                uint16 **ppsetup,
1707                                                uint32 setup_count,
1708                                                char **ppparams,
1709                                                uint32 parameter_count,
1710                                                char **ppdata,
1711                                                uint32 data_count,
1712                                                uint32 max_data_count)
1713 {
1714         char *params= *ppparams;
1715         char *data = *ppdata;
1716         files_struct *fsp = NULL;
1717         uint32 security_info_sent = 0;
1718         NTSTATUS status;
1719
1720         if(parameter_count < 8) {
1721                 reply_doserror(req, ERRDOS, ERRbadfunc);
1722                 return;
1723         }
1724
1725         if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1726                 reply_doserror(req, ERRDOS, ERRbadfid);
1727                 return;
1728         }
1729
1730         if(!lp_nt_acl_support(SNUM(conn))) {
1731                 goto done;
1732         }
1733
1734         security_info_sent = IVAL(params,4);
1735
1736         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1737                 (unsigned int)security_info_sent ));
1738
1739         if (data_count == 0) {
1740                 reply_doserror(req, ERRDOS, ERRnoaccess);
1741                 return;
1742         }
1743
1744         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1745
1746         if (!NT_STATUS_IS_OK(status)) {
1747                 reply_nterror(req, status);
1748                 return;
1749         }
1750
1751   done:
1752         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1753         return;
1754 }
1755
1756 /****************************************************************************
1757  Reply to NT IOCTL
1758 ****************************************************************************/
1759
1760 static void call_nt_transact_ioctl(connection_struct *conn,
1761                                    struct smb_request *req,
1762                                    uint16 **ppsetup, uint32 setup_count,
1763                                    char **ppparams, uint32 parameter_count,
1764                                    char **ppdata, uint32 data_count,
1765                                    uint32 max_data_count)
1766 {
1767         uint32 function;
1768         uint16 fidnum;
1769         files_struct *fsp;
1770         uint8 isFSctl;
1771         uint8 compfilter;
1772         static bool logged_message;
1773         char *pdata = *ppdata;
1774
1775         if (setup_count != 8) {
1776                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1777                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1778                 return;
1779         }
1780
1781         function = IVAL(*ppsetup, 0);
1782         fidnum = SVAL(*ppsetup, 4);
1783         isFSctl = CVAL(*ppsetup, 6);
1784         compfilter = CVAL(*ppsetup, 7);
1785
1786         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1787                  function, fidnum, isFSctl, compfilter));
1788
1789         fsp=file_fsp(fidnum);
1790         /* this check is done in each implemented function case for now
1791            because I don't want to break anything... --metze
1792         FSP_BELONGS_CONN(fsp,conn);*/
1793
1794         switch (function) {
1795         case FSCTL_SET_SPARSE:
1796                 /* pretend this succeeded - tho strictly we should
1797                    mark the file sparse (if the local fs supports it)
1798                    so we can know if we need to pre-allocate or not */
1799
1800                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1801                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1802                 return;
1803
1804         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1805         {
1806                 unsigned char objid[16];
1807
1808                 /* This should return the object-id on this file.
1809                  * I think I'll make this be the inode+dev. JRA.
1810                  */
1811
1812                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1813
1814                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1815                         return;
1816                 }
1817
1818                 data_count = 64;
1819                 pdata = nttrans_realloc(ppdata, data_count);
1820                 if (pdata == NULL) {
1821                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1822                         return;
1823                 }
1824                 push_file_id_16(pdata, &fsp->file_id);
1825                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1826                 push_file_id_16(pdata+32, &fsp->file_id);
1827                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1828                                 pdata, data_count);
1829                 return;
1830         }
1831
1832         case FSCTL_GET_REPARSE_POINT:
1833                 /* pretend this fail - my winXP does it like this
1834                  * --metze
1835                  */
1836
1837                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1838                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1839                 return;
1840
1841         case FSCTL_SET_REPARSE_POINT:
1842                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1843                  * --metze
1844                  */
1845
1846                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1847                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1848                 return;
1849
1850         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1851         {
1852                 /*
1853                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1854                  * and return their volume names.  If max_data_count is 16, then it is just
1855                  * asking for the number of volumes and length of the combined names.
1856                  *
1857                  * pdata is the data allocated by our caller, but that uses
1858                  * total_data_count (which is 0 in our case) rather than max_data_count.
1859                  * Allocate the correct amount and return the pointer to let
1860                  * it be deallocated when we return.
1861                  */
1862                 SHADOW_COPY_DATA *shadow_data = NULL;
1863                 TALLOC_CTX *shadow_mem_ctx = NULL;
1864                 bool labels = False;
1865                 uint32 labels_data_count = 0;
1866                 uint32 i;
1867                 char *cur_pdata;
1868
1869                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1870                         return;
1871                 }
1872
1873                 if (max_data_count < 16) {
1874                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1875                                 max_data_count));
1876                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1877                         return;
1878                 }
1879
1880                 if (max_data_count > 16) {
1881                         labels = True;
1882                 }
1883
1884                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1885                 if (shadow_mem_ctx == NULL) {
1886                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1887                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1888                         return;
1889                 }
1890
1891                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1892                 if (shadow_data == NULL) {
1893                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
1894                         talloc_destroy(shadow_mem_ctx);
1895                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1896                         return;
1897                 }
1898
1899                 shadow_data->mem_ctx = shadow_mem_ctx;
1900
1901                 /*
1902                  * Call the VFS routine to actually do the work.
1903                  */
1904                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1905                         talloc_destroy(shadow_data->mem_ctx);
1906                         if (errno == ENOSYS) {
1907                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
1908                                         conn->connectpath));
1909                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1910                                 return;
1911                         } else {
1912                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
1913                                         conn->connectpath));
1914                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1915                                 return;
1916                         }
1917                 }
1918
1919                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1920
1921                 if (!labels) {
1922                         data_count = 16;
1923                 } else {
1924                         data_count = 12+labels_data_count+4;
1925                 }
1926
1927                 if (max_data_count<data_count) {
1928                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1929                                 max_data_count,data_count));
1930                         talloc_destroy(shadow_data->mem_ctx);
1931                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1932                         return;
1933                 }
1934
1935                 pdata = nttrans_realloc(ppdata, data_count);
1936                 if (pdata == NULL) {
1937                         talloc_destroy(shadow_data->mem_ctx);
1938                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1939                         return;
1940                 }
1941
1942                 cur_pdata = pdata;
1943
1944                 /* num_volumes 4 bytes */
1945                 SIVAL(pdata,0,shadow_data->num_volumes);
1946
1947                 if (labels) {
1948                         /* num_labels 4 bytes */
1949                         SIVAL(pdata,4,shadow_data->num_volumes);
1950                 }
1951
1952                 /* needed_data_count 4 bytes */
1953                 SIVAL(pdata,8,labels_data_count);
1954
1955                 cur_pdata+=12;
1956
1957                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1958                         shadow_data->num_volumes,fsp->fsp_name));
1959                 if (labels && shadow_data->labels) {
1960                         for (i=0;i<shadow_data->num_volumes;i++) {
1961                                 srvstr_push(pdata, req->flags2,
1962                                             cur_pdata, shadow_data->labels[i],
1963                                             2*sizeof(SHADOW_COPY_LABEL),
1964                                             STR_UNICODE|STR_TERMINATE);
1965                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1966                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1967                         }
1968                 }
1969
1970                 talloc_destroy(shadow_data->mem_ctx);
1971
1972                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1973                                 pdata, data_count);
1974
1975                 return;
1976         }
1977
1978         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1979         {
1980                 /* pretend this succeeded -
1981                  *
1982                  * we have to send back a list with all files owned by this SID
1983                  *
1984                  * but I have to check that --metze
1985                  */
1986                 DOM_SID sid;
1987                 uid_t uid;
1988                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1989
1990                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1991
1992                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1993                         return;
1994                 }
1995
1996                 /* unknown 4 bytes: this is not the length of the sid :-(  */
1997                 /*unknown = IVAL(pdata,0);*/
1998
1999                 sid_parse(pdata+4,sid_len,&sid);
2000                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2001
2002                 if (!sid_to_uid(&sid, &uid)) {
2003                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2004                                  sid_string_dbg(&sid),
2005                                  (unsigned long)sid_len));
2006                         uid = (-1);
2007                 }
2008
2009                 /* we can take a look at the find source :-)
2010                  *
2011                  * find ./ -uid $uid  -name '*'   is what we need here
2012                  *
2013                  *
2014                  * and send 4bytes len and then NULL terminated unicode strings
2015                  * for each file
2016                  *
2017                  * but I don't know how to deal with the paged results
2018                  * (maybe we can hang the result anywhere in the fsp struct)
2019                  *
2020                  * we don't send all files at once
2021                  * and at the next we should *not* start from the beginning,
2022                  * so we have to cache the result
2023                  *
2024                  * --metze
2025                  */
2026
2027                 /* this works for now... */
2028                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2029                 return;
2030         }
2031         default:
2032                 if (!logged_message) {
2033                         logged_message = True; /* Only print this once... */
2034                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2035                                  function));
2036                 }
2037         }
2038
2039         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2040 }
2041
2042
2043 #ifdef HAVE_SYS_QUOTAS
2044 /****************************************************************************
2045  Reply to get user quota
2046 ****************************************************************************/
2047
2048 static void call_nt_transact_get_user_quota(connection_struct *conn,
2049                                             struct smb_request *req,
2050                                             uint16 **ppsetup,
2051                                             uint32 setup_count,
2052                                             char **ppparams,
2053                                             uint32 parameter_count,
2054                                             char **ppdata,
2055                                             uint32 data_count,
2056                                             uint32 max_data_count)
2057 {
2058         NTSTATUS nt_status = NT_STATUS_OK;
2059         char *params = *ppparams;
2060         char *pdata = *ppdata;
2061         char *entry;
2062         int data_len=0,param_len=0;
2063         int qt_len=0;
2064         int entry_len = 0;
2065         files_struct *fsp = NULL;
2066         uint16 level = 0;
2067         size_t sid_len;
2068         DOM_SID sid;
2069         bool start_enum = True;
2070         SMB_NTQUOTA_STRUCT qt;
2071         SMB_NTQUOTA_LIST *tmp_list;
2072         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2073
2074         ZERO_STRUCT(qt);
2075
2076         /* access check */
2077         if (current_user.ut.uid != 0) {
2078                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2079                         lp_servicename(SNUM(conn)),conn->user));
2080                 reply_doserror(req, ERRDOS, ERRnoaccess);
2081                 return;
2082         }
2083
2084         /*
2085          * Ensure minimum number of parameters sent.
2086          */
2087
2088         if (parameter_count < 4) {
2089                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2090                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2091                 return;
2092         }
2093
2094         /* maybe we can check the quota_fnum */
2095         fsp = file_fsp(SVAL(params,0));
2096         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2097                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2098                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2099                 return;
2100         }
2101
2102         /* the NULL pointer checking for fsp->fake_file_handle->pd
2103          * is done by CHECK_NTQUOTA_HANDLE_OK()
2104          */
2105         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2106
2107         level = SVAL(params,2);
2108
2109         /* unknown 12 bytes leading in params */
2110
2111         switch (level) {
2112                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2113                         /* seems that we should continue with the enum here --metze */
2114
2115                         if (qt_handle->quota_list!=NULL &&
2116                             qt_handle->tmp_list==NULL) {
2117
2118                                 /* free the list */
2119                                 free_ntquota_list(&(qt_handle->quota_list));
2120
2121                                 /* Realloc the size of parameters and data we will return */
2122                                 param_len = 4;
2123                                 params = nttrans_realloc(ppparams, param_len);
2124                                 if(params == NULL) {
2125                                         reply_doserror(req, ERRDOS, ERRnomem);
2126                                         return;
2127                                 }
2128
2129                                 data_len = 0;
2130                                 SIVAL(params,0,data_len);
2131
2132                                 break;
2133                         }
2134
2135                         start_enum = False;
2136
2137                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2138
2139                         if (qt_handle->quota_list==NULL &&
2140                                 qt_handle->tmp_list==NULL) {
2141                                 start_enum = True;
2142                         }
2143
2144                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2145                                 reply_doserror(req, ERRSRV, ERRerror);
2146                                 return;
2147                         }
2148
2149                         /* Realloc the size of parameters and data we will return */
2150                         param_len = 4;
2151                         params = nttrans_realloc(ppparams, param_len);
2152                         if(params == NULL) {
2153                                 reply_doserror(req, ERRDOS, ERRnomem);
2154                                 return;
2155                         }
2156
2157                         /* we should not trust the value in max_data_count*/
2158                         max_data_count = MIN(max_data_count,2048);
2159
2160                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2161                         if(pdata == NULL) {
2162                                 reply_doserror(req, ERRDOS, ERRnomem);
2163                                 return;
2164                         }
2165
2166                         entry = pdata;
2167
2168                         /* set params Size of returned Quota Data 4 bytes*/
2169                         /* but set it later when we know it */
2170
2171                         /* for each entry push the data */
2172
2173                         if (start_enum) {
2174                                 qt_handle->tmp_list = qt_handle->quota_list;
2175                         }
2176
2177                         tmp_list = qt_handle->tmp_list;
2178
2179                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2180                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2181
2182                                 sid_len = ndr_size_dom_sid(
2183                                         &tmp_list->quotas->sid, 0);
2184                                 entry_len = 40 + sid_len;
2185
2186                                 /* nextoffset entry 4 bytes */
2187                                 SIVAL(entry,0,entry_len);
2188
2189                                 /* then the len of the SID 4 bytes */
2190                                 SIVAL(entry,4,sid_len);
2191
2192                                 /* unknown data 8 bytes SMB_BIG_UINT */
2193                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2194
2195                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2196                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2197
2198                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2199                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2200
2201                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2202                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2203
2204                                 /* and now the SID */
2205                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2206                         }
2207
2208                         qt_handle->tmp_list = tmp_list;
2209
2210                         /* overwrite the offset of the last entry */
2211                         SIVAL(entry-entry_len,0,0);
2212
2213                         data_len = 4+qt_len;
2214                         /* overwrite the params quota_data_len */
2215                         SIVAL(params,0,data_len);
2216
2217                         break;
2218
2219                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2220
2221                         /* unknown 4 bytes IVAL(pdata,0) */
2222
2223                         if (data_count < 8) {
2224                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2225                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2226                                 return;
2227                         }
2228
2229                         sid_len = IVAL(pdata,4);
2230                         /* Ensure this is less than 1mb. */
2231                         if (sid_len > (1024*1024)) {
2232                                 reply_doserror(req, ERRDOS, ERRnomem);
2233                                 return;
2234                         }
2235
2236                         if (data_count < 8+sid_len) {
2237                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2238                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2239                                 return;
2240                         }
2241
2242                         data_len = 4+40+sid_len;
2243
2244                         if (max_data_count < data_len) {
2245                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2246                                         max_data_count, data_len));
2247                                 param_len = 4;
2248                                 SIVAL(params,0,data_len);
2249                                 data_len = 0;
2250                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2251                                 break;
2252                         }
2253
2254                         sid_parse(pdata+8,sid_len,&sid);
2255
2256                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2257                                 ZERO_STRUCT(qt);
2258                                 /*
2259                                  * we have to return zero's in all fields
2260                                  * instead of returning an error here
2261                                  * --metze
2262                                  */
2263                         }
2264
2265                         /* Realloc the size of parameters and data we will return */
2266                         param_len = 4;
2267                         params = nttrans_realloc(ppparams, param_len);
2268                         if(params == NULL) {
2269                                 reply_doserror(req, ERRDOS, ERRnomem);
2270                                 return;
2271                         }
2272
2273                         pdata = nttrans_realloc(ppdata, data_len);
2274                         if(pdata == NULL) {
2275                                 reply_doserror(req, ERRDOS, ERRnomem);
2276                                 return;
2277                         }
2278
2279                         entry = pdata;
2280
2281                         /* set params Size of returned Quota Data 4 bytes*/
2282                         SIVAL(params,0,data_len);
2283
2284                         /* nextoffset entry 4 bytes */
2285                         SIVAL(entry,0,0);
2286
2287                         /* then the len of the SID 4 bytes */
2288                         SIVAL(entry,4,sid_len);
2289
2290                         /* unknown data 8 bytes SMB_BIG_UINT */
2291                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2292
2293                         /* the used disk space 8 bytes SMB_BIG_UINT */
2294                         SBIG_UINT(entry,16,qt.usedspace);
2295
2296                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2297                         SBIG_UINT(entry,24,qt.softlim);
2298
2299                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2300                         SBIG_UINT(entry,32,qt.hardlim);
2301
2302                         /* and now the SID */
2303                         sid_linearize(entry+40, sid_len, &sid);
2304
2305                         break;
2306
2307                 default:
2308                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2309                         reply_doserror(req, ERRSRV, ERRerror);
2310                         return;
2311                         break;
2312         }
2313
2314         send_nt_replies(conn, req, nt_status, params, param_len,
2315                         pdata, data_len);
2316 }
2317
2318 /****************************************************************************
2319  Reply to set user quota
2320 ****************************************************************************/
2321
2322 static void call_nt_transact_set_user_quota(connection_struct *conn,
2323                                             struct smb_request *req,
2324                                             uint16 **ppsetup,
2325                                             uint32 setup_count,
2326                                             char **ppparams,
2327                                             uint32 parameter_count,
2328                                             char **ppdata,
2329                                             uint32 data_count,
2330                                             uint32 max_data_count)
2331 {
2332         char *params = *ppparams;
2333         char *pdata = *ppdata;
2334         int data_len=0,param_len=0;
2335         SMB_NTQUOTA_STRUCT qt;
2336         size_t sid_len;
2337         DOM_SID sid;
2338         files_struct *fsp = NULL;
2339
2340         ZERO_STRUCT(qt);
2341
2342         /* access check */
2343         if (current_user.ut.uid != 0) {
2344                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2345                         lp_servicename(SNUM(conn)),conn->user));
2346                 reply_doserror(req, ERRDOS, ERRnoaccess);
2347                 return;
2348         }
2349
2350         /*
2351          * Ensure minimum number of parameters sent.
2352          */
2353
2354         if (parameter_count < 2) {
2355                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2356                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2357                 return;
2358         }
2359
2360         /* maybe we can check the quota_fnum */
2361         fsp = file_fsp(SVAL(params,0));
2362         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2363                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2364                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2365                 return;
2366         }
2367
2368         if (data_count < 40) {
2369                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2370                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2371                 return;
2372         }
2373
2374         /* offset to next quota record.
2375          * 4 bytes IVAL(pdata,0)
2376          * unused here...
2377          */
2378
2379         /* sid len */
2380         sid_len = IVAL(pdata,4);
2381
2382         if (data_count < 40+sid_len) {
2383                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2384                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2385                 return;
2386         }
2387
2388         /* unknown 8 bytes in pdata
2389          * maybe its the change time in NTTIME
2390          */
2391
2392         /* the used space 8 bytes (SMB_BIG_UINT)*/
2393         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2394 #ifdef LARGE_SMB_OFF_T
2395         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2396 #else /* LARGE_SMB_OFF_T */
2397         if ((IVAL(pdata,20) != 0)&&
2398                 ((qt.usedspace != 0xFFFFFFFF)||
2399                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2400                 /* more than 32 bits? */
2401                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2402                 return;
2403         }
2404 #endif /* LARGE_SMB_OFF_T */
2405
2406         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2407         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2408 #ifdef LARGE_SMB_OFF_T
2409         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2410 #else /* LARGE_SMB_OFF_T */
2411         if ((IVAL(pdata,28) != 0)&&
2412                 ((qt.softlim != 0xFFFFFFFF)||
2413                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2414                 /* more than 32 bits? */
2415                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2416                 return;
2417         }
2418 #endif /* LARGE_SMB_OFF_T */
2419
2420         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2421         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2422 #ifdef LARGE_SMB_OFF_T
2423         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2424 #else /* LARGE_SMB_OFF_T */
2425         if ((IVAL(pdata,36) != 0)&&
2426                 ((qt.hardlim != 0xFFFFFFFF)||
2427                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2428                 /* more than 32 bits? */
2429                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2430                 return;
2431         }
2432 #endif /* LARGE_SMB_OFF_T */
2433
2434         sid_parse(pdata+40,sid_len,&sid);
2435         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2436
2437         /* 44 unknown bytes left... */
2438
2439         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2440                 reply_doserror(req, ERRSRV, ERRerror);
2441                 return;
2442         }
2443
2444         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2445                         pdata, data_len);
2446 }
2447 #endif /* HAVE_SYS_QUOTAS */
2448
2449 static void handle_nttrans(connection_struct *conn,
2450                            struct trans_state *state,
2451                            struct smb_request *req)
2452 {
2453         if (Protocol >= PROTOCOL_NT1) {
2454                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2455                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2456         }
2457
2458         /* Now we must call the relevant NT_TRANS function */
2459         switch(state->call) {
2460                 case NT_TRANSACT_CREATE:
2461                 {
2462                         START_PROFILE(NT_transact_create);
2463                         call_nt_transact_create(
2464                                 conn, req,
2465                                 &state->setup, state->setup_count,
2466                                 &state->param, state->total_param,
2467                                 &state->data, state->total_data,
2468                                 state->max_data_return);
2469                         END_PROFILE(NT_transact_create);
2470                         break;
2471                 }
2472
2473                 case NT_TRANSACT_IOCTL:
2474                 {
2475                         START_PROFILE(NT_transact_ioctl);
2476                         call_nt_transact_ioctl(
2477                                 conn, req,
2478                                 &state->setup, state->setup_count,
2479                                 &state->param, state->total_param,
2480                                 &state->data, state->total_data,
2481                                 state->max_data_return);
2482                         END_PROFILE(NT_transact_ioctl);
2483                         break;
2484                 }
2485
2486                 case NT_TRANSACT_SET_SECURITY_DESC:
2487                 {
2488                         START_PROFILE(NT_transact_set_security_desc);
2489                         call_nt_transact_set_security_desc(
2490                                 conn, req,
2491                                 &state->setup, state->setup_count,
2492                                 &state->param, state->total_param,
2493                                 &state->data, state->total_data,
2494                                 state->max_data_return);
2495                         END_PROFILE(NT_transact_set_security_desc);
2496                         break;
2497                 }
2498
2499                 case NT_TRANSACT_NOTIFY_CHANGE:
2500                 {
2501                         START_PROFILE(NT_transact_notify_change);
2502                         call_nt_transact_notify_change(
2503                                 conn, req,
2504                                 &state->setup, state->setup_count,
2505                                 &state->param, state->total_param,
2506                                 &state->data, state->total_data,
2507                                 state->max_data_return,
2508                                 state->max_param_return);
2509                         END_PROFILE(NT_transact_notify_change);
2510                         break;
2511                 }
2512
2513                 case NT_TRANSACT_RENAME:
2514                 {
2515                         START_PROFILE(NT_transact_rename);
2516                         call_nt_transact_rename(
2517                                 conn, req,
2518                                 &state->setup, state->setup_count,
2519                                 &state->param, state->total_param,
2520                                 &state->data, state->total_data,
2521                                 state->max_data_return);
2522                         END_PROFILE(NT_transact_rename);
2523                         break;
2524                 }
2525
2526                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2527                 {
2528                         START_PROFILE(NT_transact_query_security_desc);
2529                         call_nt_transact_query_security_desc(
2530                                 conn, req,
2531                                 &state->setup, state->setup_count,
2532                                 &state->param, state->total_param,
2533                                 &state->data, state->total_data,
2534                                 state->max_data_return);
2535                         END_PROFILE(NT_transact_query_security_desc);
2536                         break;
2537                 }
2538
2539 #ifdef HAVE_SYS_QUOTAS
2540                 case NT_TRANSACT_GET_USER_QUOTA:
2541                 {
2542                         START_PROFILE(NT_transact_get_user_quota);
2543                         call_nt_transact_get_user_quota(
2544                                 conn, req,
2545                                 &state->setup, state->setup_count,
2546                                 &state->param, state->total_param,
2547                                 &state->data, state->total_data,
2548                                 state->max_data_return);
2549                         END_PROFILE(NT_transact_get_user_quota);
2550                         break;
2551                 }
2552
2553                 case NT_TRANSACT_SET_USER_QUOTA:
2554                 {
2555                         START_PROFILE(NT_transact_set_user_quota);
2556                         call_nt_transact_set_user_quota(
2557                                 conn, req,
2558                                 &state->setup, state->setup_count,
2559                                 &state->param, state->total_param,
2560                                 &state->data, state->total_data,
2561                                 state->max_data_return);
2562                         END_PROFILE(NT_transact_set_user_quota);
2563                         break;
2564                 }
2565 #endif /* HAVE_SYS_QUOTAS */
2566
2567                 default:
2568                         /* Error in request */
2569                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2570                                  "nttrans call\n", state->call));
2571                         reply_doserror(req, ERRSRV, ERRerror);
2572                         return;
2573         }
2574         return;
2575 }
2576
2577 /****************************************************************************
2578  Reply to a SMBNTtrans.
2579 ****************************************************************************/
2580
2581 void reply_nttrans(connection_struct *conn, struct smb_request *req)
2582 {
2583         uint32 pscnt;
2584         uint32 psoff;
2585         uint32 dscnt;
2586         uint32 dsoff;
2587         uint16 function_code;
2588         NTSTATUS result;
2589         struct trans_state *state;
2590         int size;
2591
2592         START_PROFILE(SMBnttrans);
2593
2594         if (req->wct < 19) {
2595                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2596                 END_PROFILE(SMBnttrans);
2597                 return;
2598         }
2599
2600         size = smb_len(req->inbuf) + 4;
2601         pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2602         psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2603         dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2604         dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2605         function_code = SVAL(req->inbuf, smb_nt_Function);
2606
2607         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2608                 reply_doserror(req, ERRSRV, ERRaccess);
2609                 END_PROFILE(SMBnttrans);
2610                 return;
2611         }
2612
2613         result = allow_new_trans(conn->pending_trans, req->mid);
2614         if (!NT_STATUS_IS_OK(result)) {
2615                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2616                 reply_nterror(req, result);
2617                 END_PROFILE(SMBnttrans);
2618                 return;
2619         }
2620
2621         if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2622                 reply_doserror(req, ERRSRV, ERRaccess);
2623                 END_PROFILE(SMBnttrans);
2624                 return;
2625         }
2626
2627         state->cmd = SMBnttrans;
2628
2629         state->mid = req->mid;
2630         state->vuid = req->vuid;
2631         state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2632         state->data = NULL;
2633         state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2634         state->param = NULL;
2635         state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2636         state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2637
2638         /* setup count is in *words* */
2639         state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2640         state->setup = NULL;
2641         state->call = function_code;
2642
2643         /*
2644          * All nttrans messages we handle have smb_wct == 19 +
2645          * state->setup_count.  Ensure this is so as a sanity check.
2646          */
2647
2648         if(req->wct != 19 + (state->setup_count/2)) {
2649                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2650                          req->wct, 19 + (state->setup_count/2)));
2651                 goto bad_param;
2652         }
2653
2654         /* Don't allow more than 128mb for each value. */
2655         if ((state->total_data > (1024*1024*128)) ||
2656             (state->total_param > (1024*1024*128))) {
2657                 reply_doserror(req, ERRDOS, ERRnomem);
2658                 END_PROFILE(SMBnttrans);
2659                 return;
2660         }
2661
2662         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2663                 goto bad_param;
2664
2665         if (state->total_data)  {
2666                 /* Can't use talloc here, the core routines do realloc on the
2667                  * params and data. */
2668                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2669                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2670                                  "bytes !\n", (unsigned int)state->total_data));
2671                         TALLOC_FREE(state);
2672                         reply_doserror(req, ERRDOS, ERRnomem);
2673                         END_PROFILE(SMBnttrans);
2674                         return;
2675                 }
2676                 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2677                         goto bad_param;
2678                 if ((smb_base(req->inbuf)+dsoff+dscnt
2679                      > (char *)req->inbuf + size) ||
2680                     (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2681                         goto bad_param;
2682
2683                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2684         }
2685
2686         if (state->total_param) {
2687                 /* Can't use talloc here, the core routines do realloc on the
2688                  * params and data. */
2689                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2690                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2691                                  "bytes !\n", (unsigned int)state->total_param));
2692                         SAFE_FREE(state->data);
2693                         TALLOC_FREE(state);
2694                         reply_doserror(req, ERRDOS, ERRnomem);
2695                         END_PROFILE(SMBnttrans);
2696                         return;
2697                 }
2698                 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2699                         goto bad_param;
2700                 if ((smb_base(req->inbuf)+psoff+pscnt
2701                      > (char *)req->inbuf + size) ||
2702                     (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2703                         goto bad_param;
2704
2705                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2706         }
2707
2708         state->received_data  = dscnt;
2709         state->received_param = pscnt;
2710
2711         if(state->setup_count > 0) {
2712                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2713                           state->setup_count));
2714                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2715                 if (state->setup == NULL) {
2716                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2717                         SAFE_FREE(state->data);
2718                         SAFE_FREE(state->param);
2719                         TALLOC_FREE(state);
2720                         reply_doserror(req, ERRDOS, ERRnomem);
2721                         END_PROFILE(SMBnttrans);
2722                         return;
2723                 }
2724
2725                 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2726                     (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2727                         goto bad_param;
2728                 }
2729                 if (smb_nt_SetupStart + state->setup_count > size) {
2730                         goto bad_param;
2731                 }
2732
2733                 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2734                         state->setup_count);
2735                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2736         }
2737
2738         if ((state->received_data == state->total_data) &&
2739             (state->received_param == state->total_param)) {
2740                 handle_nttrans(conn, state, req);
2741                 SAFE_FREE(state->param);
2742                 SAFE_FREE(state->data);
2743                 TALLOC_FREE(state);
2744                 END_PROFILE(SMBnttrans);
2745                 return;
2746         }
2747
2748         DLIST_ADD(conn->pending_trans, state);
2749
2750         /* We need to send an interim response then receive the rest
2751            of the parameter/data bytes */
2752         reply_outbuf(req, 0, 0);
2753         show_msg((char *)req->outbuf);
2754         END_PROFILE(SMBnttrans);
2755         return;
2756
2757   bad_param:
2758
2759         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2760         SAFE_FREE(state->data);
2761         SAFE_FREE(state->param);
2762         TALLOC_FREE(state);
2763         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2764         END_PROFILE(SMBnttrans);
2765         return;
2766 }
2767
2768 /****************************************************************************
2769  Reply to a SMBnttranss
2770  ****************************************************************************/
2771
2772 void reply_nttranss(connection_struct *conn, struct smb_request *req)
2773 {
2774         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2775         struct trans_state *state;
2776
2777         int size;
2778
2779         START_PROFILE(SMBnttranss);
2780
2781         show_msg((char *)req->inbuf);
2782
2783         if (req->wct < 18) {
2784                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2785                 END_PROFILE(SMBnttranss);
2786                 return;
2787         }
2788
2789         for (state = conn->pending_trans; state != NULL;
2790              state = state->next) {
2791                 if (state->mid == req->mid) {
2792                         break;
2793                 }
2794         }
2795
2796         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2797                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2798                 END_PROFILE(SMBnttranss);
2799                 return;
2800         }
2801
2802         /* Revise state->total_param and state->total_data in case they have
2803            changed downwards */
2804         if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2805             < state->total_param) {
2806                 state->total_param = IVAL(req->inbuf,
2807                                           smb_nts_TotalParameterCount);
2808         }
2809         if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2810                 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2811         }
2812
2813         size = smb_len(req->inbuf) + 4;
2814
2815         pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2816         poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2817         pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2818
2819         dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2820         ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2821         doff = IVAL(req->inbuf, smb_nts_DataOffset);
2822
2823         state->received_param += pcnt;
2824         state->received_data += dcnt;
2825
2826         if ((state->received_data > state->total_data) ||
2827             (state->received_param > state->total_param))
2828                 goto bad_param;
2829
2830         if (pcnt) {
2831                 if (pdisp+pcnt > state->total_param)
2832                         goto bad_param;
2833                 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2834                         goto bad_param;
2835                 if (pdisp > state->total_param)
2836                         goto bad_param;
2837                 if ((smb_base(req->inbuf) + poff + pcnt
2838                      > (char *)req->inbuf + size) ||
2839                     (smb_base(req->inbuf) + poff + pcnt
2840                      < smb_base(req->inbuf)))
2841                         goto bad_param;
2842                 if (state->param + pdisp < state->param)
2843                         goto bad_param;
2844
2845                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2846                        pcnt);
2847         }
2848
2849         if (dcnt) {
2850                 if (ddisp+dcnt > state->total_data)
2851                         goto bad_param;
2852                 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2853                         goto bad_param;
2854                 if (ddisp > state->total_data)
2855                         goto bad_param;
2856                 if ((smb_base(req->inbuf) + doff + dcnt
2857                      > (char *)req->inbuf + size) ||
2858                     (smb_base(req->inbuf) + doff + dcnt
2859                      < smb_base(req->inbuf)))
2860                         goto bad_param;
2861                 if (state->data + ddisp < state->data)
2862                         goto bad_param;
2863
2864                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2865                        dcnt);
2866         }
2867
2868         if ((state->received_param < state->total_param) ||
2869             (state->received_data < state->total_data)) {
2870                 END_PROFILE(SMBnttranss);
2871                 return;
2872         }
2873
2874         /*
2875          * construct_reply_common will copy smb_com from inbuf to
2876          * outbuf. SMBnttranss is wrong here.
2877          */
2878         SCVAL(req->inbuf,smb_com,SMBnttrans);
2879
2880         handle_nttrans(conn, state, req);
2881
2882         DLIST_REMOVE(conn->pending_trans, state);
2883         SAFE_FREE(state->data);
2884         SAFE_FREE(state->param);
2885         TALLOC_FREE(state);
2886         END_PROFILE(SMBnttranss);
2887         return;
2888
2889   bad_param:
2890
2891         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2892         DLIST_REMOVE(conn->pending_trans, state);
2893         SAFE_FREE(state->data);
2894         SAFE_FREE(state->param);
2895         TALLOC_FREE(state);
2896         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2897         END_PROFILE(SMBnttranss);
2898         return;
2899 }