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