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