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