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