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