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