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