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