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