Remove all calls to reply_doserror - turn them into
[nivanova/samba-autobuild/.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 || !lp_nt_acl_support(SNUM(fsp->conn))) {
837                 return NT_STATUS_OK;
838         }
839
840         status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
841
842         if (!NT_STATUS_IS_OK(status)) {
843                 return status;
844         }
845
846         if (psd->owner_sid == NULL) {
847                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
848         }
849         if (psd->group_sid == NULL) {
850                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
851         }
852
853         /* Convert all the generic bits. */
854         security_acl_map_generic(psd->dacl, &file_generic_mapping);
855         security_acl_map_generic(psd->sacl, &file_generic_mapping);
856
857         if (DEBUGLEVEL >= 10) {
858                 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
859                 NDR_PRINT_DEBUG(security_descriptor, psd);
860         }
861
862         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
863
864         TALLOC_FREE(psd);
865
866         return status;
867 }
868
869 /****************************************************************************
870  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
871 ****************************************************************************/
872
873 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
874 {
875         struct ea_list *ea_list_head = NULL;
876         size_t offset = 0;
877
878         if (data_size < 4) {
879                 return NULL;
880         }
881
882         while (offset + 4 <= data_size) {
883                 size_t next_offset = IVAL(pdata,offset);
884                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
885
886                 if (!eal) {
887                         return NULL;
888                 }
889
890                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
891                 if (next_offset == 0) {
892                         break;
893                 }
894                 offset += next_offset;
895         }
896
897         return ea_list_head;
898 }
899
900 /****************************************************************************
901  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
902 ****************************************************************************/
903
904 static void call_nt_transact_create(connection_struct *conn,
905                                     struct smb_request *req,
906                                     uint16 **ppsetup, uint32 setup_count,
907                                     char **ppparams, uint32 parameter_count,
908                                     char **ppdata, uint32 data_count,
909                                     uint32 max_data_count)
910 {
911         struct smb_filename *smb_fname = NULL;
912         char *fname = NULL;
913         char *params = *ppparams;
914         char *data = *ppdata;
915         /* Breakout the oplock request bits so we can set the reply bits separately. */
916         uint32 fattr=0;
917         SMB_OFF_T file_len = 0;
918         int info = 0;
919         files_struct *fsp = NULL;
920         char *p = NULL;
921         uint32 flags;
922         uint32 access_mask;
923         uint32 file_attributes;
924         uint32 share_access;
925         uint32 create_disposition;
926         uint32 create_options;
927         uint32 sd_len;
928         struct security_descriptor *sd = NULL;
929         uint32 ea_len;
930         uint16 root_dir_fid;
931         struct timespec create_timespec;
932         struct timespec c_timespec;
933         struct timespec a_timespec;
934         struct timespec m_timespec;
935         struct timespec write_time_ts;
936         struct ea_list *ea_list = NULL;
937         NTSTATUS status;
938         size_t param_len;
939         uint64_t allocation_size;
940         int oplock_request;
941         uint8_t oplock_granted;
942         struct case_semantics_state *case_state = NULL;
943         TALLOC_CTX *ctx = talloc_tos();
944
945         DEBUG(5,("call_nt_transact_create\n"));
946
947         /*
948          * If it's an IPC, use the pipe handler.
949          */
950
951         if (IS_IPC(conn)) {
952                 if (lp_nt_pipe_support()) {
953                         do_nt_transact_create_pipe(
954                                 conn, req,
955                                 ppsetup, setup_count,
956                                 ppparams, parameter_count,
957                                 ppdata, data_count);
958                         goto out;
959                 }
960                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
961                 goto out;
962         }
963
964         /*
965          * Ensure minimum number of parameters sent.
966          */
967
968         if(parameter_count < 54) {
969                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
970                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
971                 goto out;
972         }
973
974         flags = IVAL(params,0);
975         access_mask = IVAL(params,8);
976         file_attributes = IVAL(params,20);
977         share_access = IVAL(params,24);
978         create_disposition = IVAL(params,28);
979         create_options = IVAL(params,32);
980         sd_len = IVAL(params,36);
981         ea_len = IVAL(params,40);
982         root_dir_fid = (uint16)IVAL(params,4);
983         allocation_size = (uint64_t)IVAL(params,12);
984 #ifdef LARGE_SMB_OFF_T
985         allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
986 #endif
987
988         /*
989          * we need to remove ignored bits when they come directly from the client
990          * because we reuse some of them for internal stuff
991          */
992         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
993
994         /* Ensure the data_len is correct for the sd and ea values given. */
995         if ((ea_len + sd_len > data_count)
996             || (ea_len > data_count) || (sd_len > data_count)
997             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
998                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
999                            "%u, data_count = %u\n", (unsigned int)ea_len,
1000                            (unsigned int)sd_len, (unsigned int)data_count));
1001                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1002                 goto out;
1003         }
1004
1005         if (sd_len) {
1006                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1007                            sd_len));
1008
1009                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1010                                              &sd);
1011                 if (!NT_STATUS_IS_OK(status)) {
1012                         DEBUG(10, ("call_nt_transact_create: "
1013                                    "unmarshall_sec_desc failed: %s\n",
1014                                    nt_errstr(status)));
1015                         reply_nterror(req, status);
1016                         goto out;
1017                 }
1018         }
1019
1020         if (ea_len) {
1021                 if (!lp_ea_support(SNUM(conn))) {
1022                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1023                                    "EA's not supported.\n",
1024                                    (unsigned int)ea_len));
1025                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1026                         goto out;
1027                 }
1028
1029                 if (ea_len < 10) {
1030                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1031                                   "too small (should be more than 10)\n",
1032                                   (unsigned int)ea_len ));
1033                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1034                         goto out;
1035                 }
1036
1037                 /* We have already checked that ea_len <= data_count here. */
1038                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1039                                                ea_len);
1040                 if (ea_list == NULL) {
1041                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1042                         goto out;
1043                 }
1044         }
1045
1046         srvstr_get_path(ctx, params, req->flags2, &fname,
1047                         params+53, parameter_count-53,
1048                         STR_TERMINATE, &status);
1049         if (!NT_STATUS_IS_OK(status)) {
1050                 reply_nterror(req, status);
1051                 goto out;
1052         }
1053
1054         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1055                 case_state = set_posix_case_semantics(ctx, conn);
1056                 if (!case_state) {
1057                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1058                         goto out;
1059                 }
1060         }
1061
1062         status = filename_convert(ctx,
1063                                 conn,
1064                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1065                                 fname,
1066                                 0,
1067                                 NULL,
1068                                 &smb_fname);
1069
1070         TALLOC_FREE(case_state);
1071
1072         if (!NT_STATUS_IS_OK(status)) {
1073                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1074                         reply_botherror(req,
1075                                 NT_STATUS_PATH_NOT_COVERED,
1076                                 ERRSRV, ERRbadpath);
1077                         goto out;
1078                 }
1079                 reply_nterror(req, status);
1080                 goto out;
1081         }
1082
1083         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1084         if (oplock_request) {
1085                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1086                         ? BATCH_OPLOCK : 0;
1087         }
1088
1089         /*
1090          * Bug #6898 - clients using Windows opens should
1091          * never be able to set this attribute into the
1092          * VFS.
1093          */
1094         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1095
1096         status = SMB_VFS_CREATE_FILE(
1097                 conn,                                   /* conn */
1098                 req,                                    /* req */
1099                 root_dir_fid,                           /* root_dir_fid */
1100                 smb_fname,                              /* fname */
1101                 access_mask,                            /* access_mask */
1102                 share_access,                           /* share_access */
1103                 create_disposition,                     /* create_disposition*/
1104                 create_options,                         /* create_options */
1105                 file_attributes,                        /* file_attributes */
1106                 oplock_request,                         /* oplock_request */
1107                 allocation_size,                        /* allocation_size */
1108                 sd,                                     /* sd */
1109                 ea_list,                                /* ea_list */
1110                 &fsp,                                   /* result */
1111                 &info);                                 /* pinfo */
1112
1113         if(!NT_STATUS_IS_OK(status)) {
1114                 if (open_was_deferred(req->mid)) {
1115                         /* We have re-scheduled this call, no error. */
1116                         return;
1117                 }
1118                 reply_openerror(req, status);
1119                 goto out;
1120         }
1121
1122         /* Ensure we're pointing at the correct stat struct. */
1123         TALLOC_FREE(smb_fname);
1124         smb_fname = fsp->fsp_name;
1125
1126         /*
1127          * If the caller set the extended oplock request bit
1128          * and we granted one (by whatever means) - set the
1129          * correct bit for extended oplock reply.
1130          */
1131
1132         if (oplock_request &&
1133             (lp_fake_oplocks(SNUM(conn))
1134              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1135
1136                 /*
1137                  * Exclusive oplock granted
1138                  */
1139
1140                 if (flags & REQUEST_BATCH_OPLOCK) {
1141                         oplock_granted = BATCH_OPLOCK_RETURN;
1142                 } else {
1143                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1144                 }
1145         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1146                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1147         } else {
1148                 oplock_granted = NO_OPLOCK_RETURN;
1149         }
1150
1151         file_len = smb_fname->st.st_ex_size;
1152
1153         /* Realloc the size of parameters and data we will return */
1154         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1155                 /* Extended response is 32 more byyes. */
1156                 param_len = 101;
1157         } else {
1158                 param_len = 69;
1159         }
1160         params = nttrans_realloc(ppparams, param_len);
1161         if(params == NULL) {
1162                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1163                 goto out;
1164         }
1165
1166         p = params;
1167         SCVAL(p, 0, oplock_granted);
1168
1169         p += 2;
1170         SSVAL(p,0,fsp->fnum);
1171         p += 2;
1172         if ((create_disposition == FILE_SUPERSEDE)
1173             && (info == FILE_WAS_OVERWRITTEN)) {
1174                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1175         } else {
1176                 SIVAL(p,0,info);
1177         }
1178         p += 8;
1179
1180         fattr = dos_mode(conn, smb_fname);
1181         if (fattr == 0) {
1182                 fattr = FILE_ATTRIBUTE_NORMAL;
1183         }
1184
1185         /* Deal with other possible opens having a modified
1186            write time. JRA. */
1187         ZERO_STRUCT(write_time_ts);
1188         get_file_infos(fsp->file_id, NULL, &write_time_ts);
1189         if (!null_timespec(write_time_ts)) {
1190                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1191         }
1192
1193         /* Create time. */
1194         create_timespec = get_create_timespec(conn, fsp, smb_fname);
1195         a_timespec = smb_fname->st.st_ex_atime;
1196         m_timespec = smb_fname->st.st_ex_mtime;
1197         c_timespec = get_change_timespec(conn, fsp, smb_fname);
1198
1199         if (lp_dos_filetime_resolution(SNUM(conn))) {
1200                 dos_filetime_timespec(&create_timespec);
1201                 dos_filetime_timespec(&a_timespec);
1202                 dos_filetime_timespec(&m_timespec);
1203                 dos_filetime_timespec(&c_timespec);
1204         }
1205
1206         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1207         p += 8;
1208         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1209         p += 8;
1210         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1211         p += 8;
1212         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1213         p += 8;
1214         SIVAL(p,0,fattr); /* File Attributes. */
1215         p += 4;
1216         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1217         p += 8;
1218         SOFF_T(p,0,file_len);
1219         p += 8;
1220         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1221                 SSVAL(p,2,0x7);
1222         }
1223         p += 4;
1224         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1225
1226         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1227                 uint32 perms = 0;
1228                 p += 25;
1229                 if (fsp->is_directory ||
1230                     can_write_to_file(conn, smb_fname)) {
1231                         perms = FILE_GENERIC_ALL;
1232                 } else {
1233                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1234                 }
1235                 SIVAL(p,0,perms);
1236         }
1237
1238         DEBUG(5,("call_nt_transact_create: open name = %s\n",
1239                  smb_fname_str_dbg(smb_fname)));
1240
1241         /* Send the required number of replies */
1242         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1243  out:
1244         return;
1245 }
1246
1247 /****************************************************************************
1248  Reply to a NT CANCEL request.
1249  conn POINTER CAN BE NULL HERE !
1250 ****************************************************************************/
1251
1252 void reply_ntcancel(struct smb_request *req)
1253 {
1254         /*
1255          * Go through and cancel any pending change notifies.
1256          */
1257
1258         START_PROFILE(SMBntcancel);
1259         srv_cancel_sign_response(smbd_server_conn);
1260         remove_pending_change_notify_requests_by_mid(req->mid);
1261         remove_pending_lock_requests_by_mid(req->mid);
1262
1263         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1264
1265         END_PROFILE(SMBntcancel);
1266         return;
1267 }
1268
1269 /****************************************************************************
1270  Copy a file.
1271 ****************************************************************************/
1272
1273 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1274                                 connection_struct *conn,
1275                                 struct smb_request *req,
1276                                 struct smb_filename *smb_fname_src,
1277                                 struct smb_filename *smb_fname_dst,
1278                                 uint32 attrs)
1279 {
1280         files_struct *fsp1,*fsp2;
1281         uint32 fattr;
1282         int info;
1283         SMB_OFF_T ret=-1;
1284         NTSTATUS status = NT_STATUS_OK;
1285         char *parent;
1286
1287         if (!CAN_WRITE(conn)) {
1288                 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1289                 goto out;
1290         }
1291
1292         /* Source must already exist. */
1293         if (!VALID_STAT(smb_fname_src->st)) {
1294                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1295                 goto out;
1296         }
1297
1298         /* Ensure attributes match. */
1299         fattr = dos_mode(conn, smb_fname_src);
1300         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1301                 status = NT_STATUS_NO_SUCH_FILE;
1302                 goto out;
1303         }
1304
1305         /* Disallow if dst file already exists. */
1306         if (VALID_STAT(smb_fname_dst->st)) {
1307                 status = NT_STATUS_OBJECT_NAME_COLLISION;
1308                 goto out;
1309         }
1310
1311         /* No links from a directory. */
1312         if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1313                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1314                 goto out;
1315         }
1316
1317         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1318                   smb_fname_str_dbg(smb_fname_src),
1319                   smb_fname_str_dbg(smb_fname_dst)));
1320
1321         status = SMB_VFS_CREATE_FILE(
1322                 conn,                                   /* conn */
1323                 req,                                    /* req */
1324                 0,                                      /* root_dir_fid */
1325                 smb_fname_src,                          /* fname */
1326                 FILE_READ_DATA,                         /* access_mask */
1327                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1328                     FILE_SHARE_DELETE),
1329                 FILE_OPEN,                              /* create_disposition*/
1330                 0,                                      /* create_options */
1331                 FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
1332                 NO_OPLOCK,                              /* oplock_request */
1333                 0,                                      /* allocation_size */
1334                 NULL,                                   /* sd */
1335                 NULL,                                   /* ea_list */
1336                 &fsp1,                                  /* result */
1337                 &info);                                 /* pinfo */
1338
1339         if (!NT_STATUS_IS_OK(status)) {
1340                 goto out;
1341         }
1342
1343         status = SMB_VFS_CREATE_FILE(
1344                 conn,                                   /* conn */
1345                 req,                                    /* req */
1346                 0,                                      /* root_dir_fid */
1347                 smb_fname_dst,                          /* fname */
1348                 FILE_WRITE_DATA,                        /* access_mask */
1349                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1350                     FILE_SHARE_DELETE),
1351                 FILE_CREATE,                            /* create_disposition*/
1352                 0,                                      /* create_options */
1353                 fattr,                                  /* file_attributes */
1354                 NO_OPLOCK,                              /* oplock_request */
1355                 0,                                      /* allocation_size */
1356                 NULL,                                   /* sd */
1357                 NULL,                                   /* ea_list */
1358                 &fsp2,                                  /* result */
1359                 &info);                                 /* pinfo */
1360
1361         if (!NT_STATUS_IS_OK(status)) {
1362                 close_file(NULL, fsp1, ERROR_CLOSE);
1363                 goto out;
1364         }
1365
1366         if (smb_fname_src->st.st_ex_size) {
1367                 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1368         }
1369
1370         /*
1371          * As we are opening fsp1 read-only we only expect
1372          * an error on close on fsp2 if we are out of space.
1373          * Thus we don't look at the error return from the
1374          * close of fsp1.
1375          */
1376         close_file(NULL, fsp1, NORMAL_CLOSE);
1377
1378         /* Ensure the modtime is set correctly on the destination file. */
1379         set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1380
1381         status = close_file(NULL, fsp2, NORMAL_CLOSE);
1382
1383         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1384            creates the file. This isn't the correct thing to do in the copy
1385            case. JRA */
1386         if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1387                             NULL)) {
1388                 status = NT_STATUS_NO_MEMORY;
1389                 goto out;
1390         }
1391         file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1392         TALLOC_FREE(parent);
1393
1394         if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1395                 status = NT_STATUS_DISK_FULL;
1396                 goto out;
1397         }
1398  out:
1399         if (!NT_STATUS_IS_OK(status)) {
1400                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1401                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1402                         smb_fname_str_dbg(smb_fname_dst)));
1403         }
1404
1405         return status;
1406 }
1407
1408 /****************************************************************************
1409  Reply to a NT rename request.
1410 ****************************************************************************/
1411
1412 void reply_ntrename(struct smb_request *req)
1413 {
1414         connection_struct *conn = req->conn;
1415         struct smb_filename *smb_fname_old = NULL;
1416         struct smb_filename *smb_fname_new = NULL;
1417         char *oldname = NULL;
1418         char *newname = NULL;
1419         const char *p;
1420         NTSTATUS status;
1421         bool src_has_wcard = False;
1422         bool dest_has_wcard = False;
1423         uint32 attrs;
1424         uint32_t ucf_flags_src = 0;
1425         uint32_t ucf_flags_dst = 0;
1426         uint16 rename_type;
1427         TALLOC_CTX *ctx = talloc_tos();
1428
1429         START_PROFILE(SMBntrename);
1430
1431         if (req->wct < 4) {
1432                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1433                 goto out;
1434         }
1435
1436         attrs = SVAL(req->vwv+0, 0);
1437         rename_type = SVAL(req->vwv+1, 0);
1438
1439         p = (const char *)req->buf + 1;
1440         p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1441                                        &status, &src_has_wcard);
1442         if (!NT_STATUS_IS_OK(status)) {
1443                 reply_nterror(req, status);
1444                 goto out;
1445         }
1446
1447         if (ms_has_wild(oldname)) {
1448                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1449                 goto out;
1450         }
1451
1452         p++;
1453         p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1454                                        &status, &dest_has_wcard);
1455         if (!NT_STATUS_IS_OK(status)) {
1456                 reply_nterror(req, status);
1457                 goto out;
1458         }
1459
1460         /* The newname must begin with a ':' if the oldname contains a ':'. */
1461         if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1462                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1463                 goto out;
1464         }
1465
1466         /*
1467          * If this is a rename operation, allow wildcards and save the
1468          * destination's last component.
1469          */
1470         if (rename_type == RENAME_FLAG_RENAME) {
1471                 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1472                 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1473         }
1474
1475         /* rename_internals() calls unix_convert(), so don't call it here. */
1476         status = filename_convert(ctx, conn,
1477                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1478                                   oldname,
1479                                   ucf_flags_src,
1480                                   NULL,
1481                                   &smb_fname_old);
1482         if (!NT_STATUS_IS_OK(status)) {
1483                 if (NT_STATUS_EQUAL(status,
1484                                     NT_STATUS_PATH_NOT_COVERED)) {
1485                         reply_botherror(req,
1486                                         NT_STATUS_PATH_NOT_COVERED,
1487                                         ERRSRV, ERRbadpath);
1488                         goto out;
1489                 }
1490                 reply_nterror(req, status);
1491                 goto out;
1492         }
1493
1494         status = filename_convert(ctx, conn,
1495                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1496                                   newname,
1497                                   ucf_flags_dst,
1498                                   &dest_has_wcard,
1499                                   &smb_fname_new);
1500         if (!NT_STATUS_IS_OK(status)) {
1501                 if (NT_STATUS_EQUAL(status,
1502                                     NT_STATUS_PATH_NOT_COVERED)) {
1503                         reply_botherror(req,
1504                                         NT_STATUS_PATH_NOT_COVERED,
1505                                         ERRSRV, ERRbadpath);
1506                         goto out;
1507                 }
1508                 reply_nterror(req, status);
1509                 goto out;
1510         }
1511
1512         DEBUG(3,("reply_ntrename: %s -> %s\n",
1513                  smb_fname_str_dbg(smb_fname_old),
1514                  smb_fname_str_dbg(smb_fname_new)));
1515
1516         switch(rename_type) {
1517                 case RENAME_FLAG_RENAME:
1518                         status = rename_internals(ctx, conn, req,
1519                                                   smb_fname_old, smb_fname_new,
1520                                                   attrs, False, src_has_wcard,
1521                                                   dest_has_wcard,
1522                                                   DELETE_ACCESS);
1523                         break;
1524                 case RENAME_FLAG_HARD_LINK:
1525                         if (src_has_wcard || dest_has_wcard) {
1526                                 /* No wildcards. */
1527                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1528                         } else {
1529                                 status = hardlink_internals(ctx, conn,
1530                                                             smb_fname_old,
1531                                                             smb_fname_new);
1532                         }
1533                         break;
1534                 case RENAME_FLAG_COPY:
1535                         if (src_has_wcard || dest_has_wcard) {
1536                                 /* No wildcards. */
1537                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1538                         } else {
1539                                 status = copy_internals(ctx, conn, req,
1540                                                         smb_fname_old,
1541                                                         smb_fname_new,
1542                                                         attrs);
1543                         }
1544                         break;
1545                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1546                         status = NT_STATUS_INVALID_PARAMETER;
1547                         break;
1548                 default:
1549                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1550                         break;
1551         }
1552
1553         if (!NT_STATUS_IS_OK(status)) {
1554                 if (open_was_deferred(req->mid)) {
1555                         /* We have re-scheduled this call. */
1556                         goto out;
1557                 }
1558
1559                 reply_nterror(req, status);
1560                 goto out;
1561         }
1562
1563         reply_outbuf(req, 0, 0);
1564  out:
1565         END_PROFILE(SMBntrename);
1566         return;
1567 }
1568
1569 /****************************************************************************
1570  Reply to a notify change - queue the request and
1571  don't allow a directory to be opened.
1572 ****************************************************************************/
1573
1574 static void smbd_smb1_notify_reply(struct smb_request *req,
1575                                    NTSTATUS error_code,
1576                                    uint8_t *buf, size_t len)
1577 {
1578         send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1579 }
1580
1581 static void call_nt_transact_notify_change(connection_struct *conn,
1582                                            struct smb_request *req,
1583                                            uint16 **ppsetup,
1584                                            uint32 setup_count,
1585                                            char **ppparams,
1586                                            uint32 parameter_count,
1587                                            char **ppdata, uint32 data_count,
1588                                            uint32 max_data_count,
1589                                            uint32 max_param_count)
1590 {
1591         uint16 *setup = *ppsetup;
1592         files_struct *fsp;
1593         uint32 filter;
1594         NTSTATUS status;
1595         bool recursive;
1596
1597         if(setup_count < 6) {
1598                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1599                 return;
1600         }
1601
1602         fsp = file_fsp(req, SVAL(setup,4));
1603         filter = IVAL(setup, 0);
1604         recursive = (SVAL(setup, 6) != 0) ? True : False;
1605
1606         DEBUG(3,("call_nt_transact_notify_change\n"));
1607
1608         if(!fsp) {
1609                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1610                 return;
1611         }
1612
1613         {
1614                 char *filter_string;
1615
1616                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1617                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1618                         return;
1619                 }
1620
1621                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1622                          "called on %s, filter = %s, recursive = %d\n",
1623                          fsp_str_dbg(fsp), filter_string, recursive));
1624
1625                 TALLOC_FREE(filter_string);
1626         }
1627
1628         if((!fsp->is_directory) || (conn != fsp->conn)) {
1629                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1630                 return;
1631         }
1632
1633         if (fsp->notify == NULL) {
1634
1635                 status = change_notify_create(fsp, filter, recursive);
1636
1637                 if (!NT_STATUS_IS_OK(status)) {
1638                         DEBUG(10, ("change_notify_create returned %s\n",
1639                                    nt_errstr(status)));
1640                         reply_nterror(req, status);
1641                         return;
1642                 }
1643         }
1644
1645         if (fsp->notify->num_changes != 0) {
1646
1647                 /*
1648                  * We've got changes pending, respond immediately
1649                  */
1650
1651                 /*
1652                  * TODO: write a torture test to check the filtering behaviour
1653                  * here.
1654                  */
1655
1656                 change_notify_reply(fsp->conn, req,
1657                                     NT_STATUS_OK,
1658                                     max_param_count,
1659                                     fsp->notify,
1660                                     smbd_smb1_notify_reply);
1661
1662                 /*
1663                  * change_notify_reply() above has independently sent its
1664                  * results
1665                  */
1666                 return;
1667         }
1668
1669         /*
1670          * No changes pending, queue the request
1671          */
1672
1673         status = change_notify_add_request(req,
1674                         max_param_count,
1675                         filter,
1676                         recursive, fsp,
1677                         smbd_smb1_notify_reply);
1678         if (!NT_STATUS_IS_OK(status)) {
1679                 reply_nterror(req, status);
1680         }
1681         return;
1682 }
1683
1684 /****************************************************************************
1685  Reply to an NT transact rename command.
1686 ****************************************************************************/
1687
1688 static void call_nt_transact_rename(connection_struct *conn,
1689                                     struct smb_request *req,
1690                                     uint16 **ppsetup, uint32 setup_count,
1691                                     char **ppparams, uint32 parameter_count,
1692                                     char **ppdata, uint32 data_count,
1693                                     uint32 max_data_count)
1694 {
1695         char *params = *ppparams;
1696         char *new_name = NULL;
1697         files_struct *fsp = NULL;
1698         bool dest_has_wcard = False;
1699         NTSTATUS status;
1700         TALLOC_CTX *ctx = talloc_tos();
1701
1702         if(parameter_count < 5) {
1703                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1704                 return;
1705         }
1706
1707         fsp = file_fsp(req, SVAL(params, 0));
1708         if (!check_fsp(conn, req, fsp)) {
1709                 return;
1710         }
1711         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1712                               parameter_count - 4,
1713                               STR_TERMINATE, &status, &dest_has_wcard);
1714         if (!NT_STATUS_IS_OK(status)) {
1715                 reply_nterror(req, status);
1716                 return;
1717         }
1718
1719         /*
1720          * W2K3 ignores this request as the RAW-RENAME test
1721          * demonstrates, so we do.
1722          */
1723         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1724
1725         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1726                  fsp_str_dbg(fsp), new_name));
1727
1728         return;
1729 }
1730
1731 /******************************************************************************
1732  Fake up a completely empty SD.
1733 *******************************************************************************/
1734
1735 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1736 {
1737         size_t sd_size;
1738
1739         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1740         if(!*ppsd) {
1741                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1742                 return NT_STATUS_NO_MEMORY;
1743         }
1744
1745         return NT_STATUS_OK;
1746 }
1747
1748 /****************************************************************************
1749  Reply to query a security descriptor.
1750 ****************************************************************************/
1751
1752 static void call_nt_transact_query_security_desc(connection_struct *conn,
1753                                                  struct smb_request *req,
1754                                                  uint16 **ppsetup,
1755                                                  uint32 setup_count,
1756                                                  char **ppparams,
1757                                                  uint32 parameter_count,
1758                                                  char **ppdata,
1759                                                  uint32 data_count,
1760                                                  uint32 max_data_count)
1761 {
1762         char *params = *ppparams;
1763         char *data = *ppdata;
1764         SEC_DESC *psd = NULL;
1765         size_t sd_size;
1766         uint32 security_info_wanted;
1767         files_struct *fsp = NULL;
1768         NTSTATUS status;
1769         DATA_BLOB blob;
1770
1771         if(parameter_count < 8) {
1772                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1773                 return;
1774         }
1775
1776         fsp = file_fsp(req, SVAL(params,0));
1777         if(!fsp) {
1778                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1779                 return;
1780         }
1781
1782         security_info_wanted = IVAL(params,4);
1783
1784         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1785                  "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1786                  (unsigned int)security_info_wanted));
1787
1788         params = nttrans_realloc(ppparams, 4);
1789         if(params == NULL) {
1790                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1791                 return;
1792         }
1793
1794         /*
1795          * Get the permissions to return.
1796          */
1797
1798         if (!lp_nt_acl_support(SNUM(conn))) {
1799                 status = get_null_nt_acl(talloc_tos(), &psd);
1800         } else {
1801                 status = SMB_VFS_FGET_NT_ACL(
1802                         fsp, security_info_wanted, &psd);
1803         }
1804         if (!NT_STATUS_IS_OK(status)) {
1805                 reply_nterror(req, status);
1806                 return;
1807         }
1808
1809         /* If the SACL/DACL is NULL, but was requested, we mark that it is
1810          * present in the reply to match Windows behavior */
1811         if (psd->sacl == NULL &&
1812             security_info_wanted & SACL_SECURITY_INFORMATION)
1813                 psd->type |= SEC_DESC_SACL_PRESENT;
1814         if (psd->dacl == NULL &&
1815             security_info_wanted & DACL_SECURITY_INFORMATION)
1816                 psd->type |= SEC_DESC_DACL_PRESENT;
1817
1818         sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1819
1820         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1821
1822         if (DEBUGLEVEL >= 10) {
1823                 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1824                           fsp_str_dbg(fsp)));
1825                 NDR_PRINT_DEBUG(security_descriptor, psd);
1826         }
1827
1828         SIVAL(params,0,(uint32)sd_size);
1829
1830         if (max_data_count < sd_size) {
1831                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1832                                 params, 4, *ppdata, 0);
1833                 return;
1834         }
1835
1836         /*
1837          * Allocate the data we will point this at.
1838          */
1839
1840         data = nttrans_realloc(ppdata, sd_size);
1841         if(data == NULL) {
1842                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1843                 return;
1844         }
1845
1846         status = marshall_sec_desc(talloc_tos(), psd,
1847                                    &blob.data, &blob.length);
1848
1849         if (!NT_STATUS_IS_OK(status)) {
1850                 reply_nterror(req, status);
1851                 return;
1852         }
1853
1854         SMB_ASSERT(sd_size == blob.length);
1855         memcpy(data, blob.data, sd_size);
1856
1857         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1858
1859         return;
1860 }
1861
1862 /****************************************************************************
1863  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1864 ****************************************************************************/
1865
1866 static void call_nt_transact_set_security_desc(connection_struct *conn,
1867                                                struct smb_request *req,
1868                                                uint16 **ppsetup,
1869                                                uint32 setup_count,
1870                                                char **ppparams,
1871                                                uint32 parameter_count,
1872                                                char **ppdata,
1873                                                uint32 data_count,
1874                                                uint32 max_data_count)
1875 {
1876         char *params= *ppparams;
1877         char *data = *ppdata;
1878         files_struct *fsp = NULL;
1879         uint32 security_info_sent = 0;
1880         NTSTATUS status;
1881
1882         if(parameter_count < 8) {
1883                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1884                 return;
1885         }
1886
1887         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1888                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1889                 return;
1890         }
1891
1892         if(!lp_nt_acl_support(SNUM(conn))) {
1893                 goto done;
1894         }
1895
1896         security_info_sent = IVAL(params,4);
1897
1898         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1899                  fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1900
1901         if (data_count == 0) {
1902                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1903                 return;
1904         }
1905
1906         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1907
1908         if (!NT_STATUS_IS_OK(status)) {
1909                 reply_nterror(req, status);
1910                 return;
1911         }
1912
1913   done:
1914         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1915         return;
1916 }
1917
1918 /****************************************************************************
1919  Reply to NT IOCTL
1920 ****************************************************************************/
1921
1922 static void call_nt_transact_ioctl(connection_struct *conn,
1923                                    struct smb_request *req,
1924                                    uint16 **ppsetup, uint32 setup_count,
1925                                    char **ppparams, uint32 parameter_count,
1926                                    char **ppdata, uint32 data_count,
1927                                    uint32 max_data_count)
1928 {
1929         uint32 function;
1930         uint16 fidnum;
1931         files_struct *fsp;
1932         uint8 isFSctl;
1933         uint8 compfilter;
1934         char *pdata = *ppdata;
1935
1936         if (setup_count != 8) {
1937                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1938                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1939                 return;
1940         }
1941
1942         function = IVAL(*ppsetup, 0);
1943         fidnum = SVAL(*ppsetup, 4);
1944         isFSctl = CVAL(*ppsetup, 6);
1945         compfilter = CVAL(*ppsetup, 7);
1946
1947         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1948                  function, fidnum, isFSctl, compfilter));
1949
1950         fsp=file_fsp(req, fidnum);
1951         /* this check is done in each implemented function case for now
1952            because I don't want to break anything... --metze
1953         FSP_BELONGS_CONN(fsp,conn);*/
1954
1955         SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1956
1957         switch (function) {
1958         case FSCTL_SET_SPARSE:
1959                 /* pretend this succeeded - tho strictly we should
1960                    mark the file sparse (if the local fs supports it)
1961                    so we can know if we need to pre-allocate or not */
1962
1963                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1964                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1965                 return;
1966
1967         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1968         {
1969                 unsigned char objid[16];
1970
1971                 /* This should return the object-id on this file.
1972                  * I think I'll make this be the inode+dev. JRA.
1973                  */
1974
1975                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1976
1977                 if (!fsp_belongs_conn(conn, req, fsp)) {
1978                         return;
1979                 }
1980
1981                 data_count = 64;
1982                 pdata = nttrans_realloc(ppdata, data_count);
1983                 if (pdata == NULL) {
1984                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1985                         return;
1986                 }
1987
1988                 /* For backwards compatibility only store the dev/inode. */
1989                 push_file_id_16(pdata, &fsp->file_id);
1990                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1991                 push_file_id_16(pdata+32, &fsp->file_id);
1992                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1993                                 pdata, data_count);
1994                 return;
1995         }
1996
1997         case FSCTL_GET_REPARSE_POINT:
1998                 /* pretend this fail - my winXP does it like this
1999                  * --metze
2000                  */
2001
2002                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2003                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2004                 return;
2005
2006         case FSCTL_SET_REPARSE_POINT:
2007                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2008                  * --metze
2009                  */
2010
2011                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2012                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2013                 return;
2014
2015         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2016         {
2017                 /*
2018                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2019                  * and return their volume names.  If max_data_count is 16, then it is just
2020                  * asking for the number of volumes and length of the combined names.
2021                  *
2022                  * pdata is the data allocated by our caller, but that uses
2023                  * total_data_count (which is 0 in our case) rather than max_data_count.
2024                  * Allocate the correct amount and return the pointer to let
2025                  * it be deallocated when we return.
2026                  */
2027                 SHADOW_COPY_DATA *shadow_data = NULL;
2028                 TALLOC_CTX *shadow_mem_ctx = NULL;
2029                 bool labels = False;
2030                 uint32 labels_data_count = 0;
2031                 uint32 i;
2032                 char *cur_pdata;
2033
2034                 if (!fsp_belongs_conn(conn, req, fsp)) {
2035                         return;
2036                 }
2037
2038                 if (max_data_count < 16) {
2039                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2040                                 max_data_count));
2041                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2042                         return;
2043                 }
2044
2045                 if (max_data_count > 16) {
2046                         labels = True;
2047                 }
2048
2049                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2050                 if (shadow_mem_ctx == NULL) {
2051                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2052                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2053                         return;
2054                 }
2055
2056                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2057                 if (shadow_data == NULL) {
2058                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
2059                         talloc_destroy(shadow_mem_ctx);
2060                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2061                         return;
2062                 }
2063
2064                 shadow_data->mem_ctx = shadow_mem_ctx;
2065
2066                 /*
2067                  * Call the VFS routine to actually do the work.
2068                  */
2069                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2070                         talloc_destroy(shadow_data->mem_ctx);
2071                         if (errno == ENOSYS) {
2072                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2073                                         conn->connectpath));
2074                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2075                                 return;
2076                         } else {
2077                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2078                                         conn->connectpath));
2079                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2080                                 return;
2081                         }
2082                 }
2083
2084                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2085
2086                 if (!labels) {
2087                         data_count = 16;
2088                 } else {
2089                         data_count = 12+labels_data_count+4;
2090                 }
2091
2092                 if (max_data_count<data_count) {
2093                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2094                                 max_data_count,data_count));
2095                         talloc_destroy(shadow_data->mem_ctx);
2096                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2097                         return;
2098                 }
2099
2100                 pdata = nttrans_realloc(ppdata, data_count);
2101                 if (pdata == NULL) {
2102                         talloc_destroy(shadow_data->mem_ctx);
2103                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2104                         return;
2105                 }
2106
2107                 cur_pdata = pdata;
2108
2109                 /* num_volumes 4 bytes */
2110                 SIVAL(pdata,0,shadow_data->num_volumes);
2111
2112                 if (labels) {
2113                         /* num_labels 4 bytes */
2114                         SIVAL(pdata,4,shadow_data->num_volumes);
2115                 }
2116
2117                 /* needed_data_count 4 bytes */
2118                 SIVAL(pdata, 8, labels_data_count+4);
2119
2120                 cur_pdata+=12;
2121
2122                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2123                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
2124                 if (labels && shadow_data->labels) {
2125                         for (i=0;i<shadow_data->num_volumes;i++) {
2126                                 srvstr_push(pdata, req->flags2,
2127                                             cur_pdata, shadow_data->labels[i],
2128                                             2*sizeof(SHADOW_COPY_LABEL),
2129                                             STR_UNICODE|STR_TERMINATE);
2130                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2131                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2132                         }
2133                 }
2134
2135                 talloc_destroy(shadow_data->mem_ctx);
2136
2137                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2138                                 pdata, data_count);
2139
2140                 return;
2141         }
2142
2143         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2144         {
2145                 /* pretend this succeeded -
2146                  *
2147                  * we have to send back a list with all files owned by this SID
2148                  *
2149                  * but I have to check that --metze
2150                  */
2151                 DOM_SID sid;
2152                 uid_t uid;
2153                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2154
2155                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2156
2157                 if (!fsp_belongs_conn(conn, req, fsp)) {
2158                         return;
2159                 }
2160
2161                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2162                 /*unknown = IVAL(pdata,0);*/
2163
2164                 sid_parse(pdata+4,sid_len,&sid);
2165                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2166
2167                 if (!sid_to_uid(&sid, &uid)) {
2168                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2169                                  sid_string_dbg(&sid),
2170                                  (unsigned long)sid_len));
2171                         uid = (-1);
2172                 }
2173
2174                 /* we can take a look at the find source :-)
2175                  *
2176                  * find ./ -uid $uid  -name '*'   is what we need here
2177                  *
2178                  *
2179                  * and send 4bytes len and then NULL terminated unicode strings
2180                  * for each file
2181                  *
2182                  * but I don't know how to deal with the paged results
2183                  * (maybe we can hang the result anywhere in the fsp struct)
2184                  *
2185                  * we don't send all files at once
2186                  * and at the next we should *not* start from the beginning,
2187                  * so we have to cache the result
2188                  *
2189                  * --metze
2190                  */
2191
2192                 /* this works for now... */
2193                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2194                 return;
2195         }
2196         default:
2197                 if (!logged_ioctl_message) {
2198                         logged_ioctl_message = true; /* Only print this once... */
2199                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2200                                  function));
2201                 }
2202         }
2203
2204         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2205 }
2206
2207
2208 #ifdef HAVE_SYS_QUOTAS
2209 /****************************************************************************
2210  Reply to get user quota
2211 ****************************************************************************/
2212
2213 static void call_nt_transact_get_user_quota(connection_struct *conn,
2214                                             struct smb_request *req,
2215                                             uint16 **ppsetup,
2216                                             uint32 setup_count,
2217                                             char **ppparams,
2218                                             uint32 parameter_count,
2219                                             char **ppdata,
2220                                             uint32 data_count,
2221                                             uint32 max_data_count)
2222 {
2223         NTSTATUS nt_status = NT_STATUS_OK;
2224         char *params = *ppparams;
2225         char *pdata = *ppdata;
2226         char *entry;
2227         int data_len=0,param_len=0;
2228         int qt_len=0;
2229         int entry_len = 0;
2230         files_struct *fsp = NULL;
2231         uint16 level = 0;
2232         size_t sid_len;
2233         DOM_SID sid;
2234         bool start_enum = True;
2235         SMB_NTQUOTA_STRUCT qt;
2236         SMB_NTQUOTA_LIST *tmp_list;
2237         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2238
2239         ZERO_STRUCT(qt);
2240
2241         /* access check */
2242         if (conn->server_info->utok.uid != 0) {
2243                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2244                          "[%s]\n", lp_servicename(SNUM(conn)),
2245                          conn->server_info->unix_name));
2246                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2247                 return;
2248         }
2249
2250         /*
2251          * Ensure minimum number of parameters sent.
2252          */
2253
2254         if (parameter_count < 4) {
2255                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2256                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2257                 return;
2258         }
2259
2260         /* maybe we can check the quota_fnum */
2261         fsp = file_fsp(req, SVAL(params,0));
2262         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2263                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2264                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2265                 return;
2266         }
2267
2268         /* the NULL pointer checking for fsp->fake_file_handle->pd
2269          * is done by CHECK_NTQUOTA_HANDLE_OK()
2270          */
2271         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2272
2273         level = SVAL(params,2);
2274
2275         /* unknown 12 bytes leading in params */
2276
2277         switch (level) {
2278                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2279                         /* seems that we should continue with the enum here --metze */
2280
2281                         if (qt_handle->quota_list!=NULL &&
2282                             qt_handle->tmp_list==NULL) {
2283
2284                                 /* free the list */
2285                                 free_ntquota_list(&(qt_handle->quota_list));
2286
2287                                 /* Realloc the size of parameters and data we will return */
2288                                 param_len = 4;
2289                                 params = nttrans_realloc(ppparams, param_len);
2290                                 if(params == NULL) {
2291                                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2292                                         return;
2293                                 }
2294
2295                                 data_len = 0;
2296                                 SIVAL(params,0,data_len);
2297
2298                                 break;
2299                         }
2300
2301                         start_enum = False;
2302
2303                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2304
2305                         if (qt_handle->quota_list==NULL &&
2306                                 qt_handle->tmp_list==NULL) {
2307                                 start_enum = True;
2308                         }
2309
2310                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2311                                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2312                                 return;
2313                         }
2314
2315                         /* Realloc the size of parameters and data we will return */
2316                         param_len = 4;
2317                         params = nttrans_realloc(ppparams, param_len);
2318                         if(params == NULL) {
2319                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2320                                 return;
2321                         }
2322
2323                         /* we should not trust the value in max_data_count*/
2324                         max_data_count = MIN(max_data_count,2048);
2325
2326                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2327                         if(pdata == NULL) {
2328                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2329                                 return;
2330                         }
2331
2332                         entry = pdata;
2333
2334                         /* set params Size of returned Quota Data 4 bytes*/
2335                         /* but set it later when we know it */
2336
2337                         /* for each entry push the data */
2338
2339                         if (start_enum) {
2340                                 qt_handle->tmp_list = qt_handle->quota_list;
2341                         }
2342
2343                         tmp_list = qt_handle->tmp_list;
2344
2345                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2346                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2347
2348                                 sid_len = ndr_size_dom_sid(
2349                                         &tmp_list->quotas->sid, NULL, 0);
2350                                 entry_len = 40 + sid_len;
2351
2352                                 /* nextoffset entry 4 bytes */
2353                                 SIVAL(entry,0,entry_len);
2354
2355                                 /* then the len of the SID 4 bytes */
2356                                 SIVAL(entry,4,sid_len);
2357
2358                                 /* unknown data 8 bytes uint64_t */
2359                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2360
2361                                 /* the used disk space 8 bytes uint64_t */
2362                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2363
2364                                 /* the soft quotas 8 bytes uint64_t */
2365                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2366
2367                                 /* the hard quotas 8 bytes uint64_t */
2368                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2369
2370                                 /* and now the SID */
2371                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2372                         }
2373
2374                         qt_handle->tmp_list = tmp_list;
2375
2376                         /* overwrite the offset of the last entry */
2377                         SIVAL(entry-entry_len,0,0);
2378
2379                         data_len = 4+qt_len;
2380                         /* overwrite the params quota_data_len */
2381                         SIVAL(params,0,data_len);
2382
2383                         break;
2384
2385                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2386
2387                         /* unknown 4 bytes IVAL(pdata,0) */
2388
2389                         if (data_count < 8) {
2390                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2391                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2392                                 return;
2393                         }
2394
2395                         sid_len = IVAL(pdata,4);
2396                         /* Ensure this is less than 1mb. */
2397                         if (sid_len > (1024*1024)) {
2398                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2399                                 return;
2400                         }
2401
2402                         if (data_count < 8+sid_len) {
2403                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2404                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2405                                 return;
2406                         }
2407
2408                         data_len = 4+40+sid_len;
2409
2410                         if (max_data_count < data_len) {
2411                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2412                                         max_data_count, data_len));
2413                                 param_len = 4;
2414                                 SIVAL(params,0,data_len);
2415                                 data_len = 0;
2416                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2417                                 break;
2418                         }
2419
2420                         sid_parse(pdata+8,sid_len,&sid);
2421
2422                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2423                                 ZERO_STRUCT(qt);
2424                                 /*
2425                                  * we have to return zero's in all fields
2426                                  * instead of returning an error here
2427                                  * --metze
2428                                  */
2429                         }
2430
2431                         /* Realloc the size of parameters and data we will return */
2432                         param_len = 4;
2433                         params = nttrans_realloc(ppparams, param_len);
2434                         if(params == NULL) {
2435                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2436                                 return;
2437                         }
2438
2439                         pdata = nttrans_realloc(ppdata, data_len);
2440                         if(pdata == NULL) {
2441                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2442                                 return;
2443                         }
2444
2445                         entry = pdata;
2446
2447                         /* set params Size of returned Quota Data 4 bytes*/
2448                         SIVAL(params,0,data_len);
2449
2450                         /* nextoffset entry 4 bytes */
2451                         SIVAL(entry,0,0);
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...-mezte*/
2458
2459                         /* the used disk space 8 bytes uint64_t */
2460                         SBIG_UINT(entry,16,qt.usedspace);
2461
2462                         /* the soft quotas 8 bytes uint64_t */
2463                         SBIG_UINT(entry,24,qt.softlim);
2464
2465                         /* the hard quotas 8 bytes uint64_t */
2466                         SBIG_UINT(entry,32,qt.hardlim);
2467
2468                         /* and now the SID */
2469                         sid_linearize(entry+40, sid_len, &sid);
2470
2471                         break;
2472
2473                 default:
2474                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2475                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2476                         return;
2477                         break;
2478         }
2479
2480         send_nt_replies(conn, req, nt_status, params, param_len,
2481                         pdata, data_len);
2482 }
2483
2484 /****************************************************************************
2485  Reply to set user quota
2486 ****************************************************************************/
2487
2488 static void call_nt_transact_set_user_quota(connection_struct *conn,
2489                                             struct smb_request *req,
2490                                             uint16 **ppsetup,
2491                                             uint32 setup_count,
2492                                             char **ppparams,
2493                                             uint32 parameter_count,
2494                                             char **ppdata,
2495                                             uint32 data_count,
2496                                             uint32 max_data_count)
2497 {
2498         char *params = *ppparams;
2499         char *pdata = *ppdata;
2500         int data_len=0,param_len=0;
2501         SMB_NTQUOTA_STRUCT qt;
2502         size_t sid_len;
2503         DOM_SID sid;
2504         files_struct *fsp = NULL;
2505
2506         ZERO_STRUCT(qt);
2507
2508         /* access check */
2509         if (conn->server_info->utok.uid != 0) {
2510                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2511                          "[%s]\n", lp_servicename(SNUM(conn)),
2512                          conn->server_info->unix_name));
2513                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2514                 return;
2515         }
2516
2517         /*
2518          * Ensure minimum number of parameters sent.
2519          */
2520
2521         if (parameter_count < 2) {
2522                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2523                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2524                 return;
2525         }
2526
2527         /* maybe we can check the quota_fnum */
2528         fsp = file_fsp(req, SVAL(params,0));
2529         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2530                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2531                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2532                 return;
2533         }
2534
2535         if (data_count < 40) {
2536                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2537                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2538                 return;
2539         }
2540
2541         /* offset to next quota record.
2542          * 4 bytes IVAL(pdata,0)
2543          * unused here...
2544          */
2545
2546         /* sid len */
2547         sid_len = IVAL(pdata,4);
2548
2549         if (data_count < 40+sid_len) {
2550                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2551                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2552                 return;
2553         }
2554
2555         /* unknown 8 bytes in pdata
2556          * maybe its the change time in NTTIME
2557          */
2558
2559         /* the used space 8 bytes (uint64_t)*/
2560         qt.usedspace = (uint64_t)IVAL(pdata,16);
2561 #ifdef LARGE_SMB_OFF_T
2562         qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2563 #else /* LARGE_SMB_OFF_T */
2564         if ((IVAL(pdata,20) != 0)&&
2565                 ((qt.usedspace != 0xFFFFFFFF)||
2566                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2567                 /* more than 32 bits? */
2568                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2569                 return;
2570         }
2571 #endif /* LARGE_SMB_OFF_T */
2572
2573         /* the soft quotas 8 bytes (uint64_t)*/
2574         qt.softlim = (uint64_t)IVAL(pdata,24);
2575 #ifdef LARGE_SMB_OFF_T
2576         qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2577 #else /* LARGE_SMB_OFF_T */
2578         if ((IVAL(pdata,28) != 0)&&
2579                 ((qt.softlim != 0xFFFFFFFF)||
2580                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2581                 /* more than 32 bits? */
2582                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2583                 return;
2584         }
2585 #endif /* LARGE_SMB_OFF_T */
2586
2587         /* the hard quotas 8 bytes (uint64_t)*/
2588         qt.hardlim = (uint64_t)IVAL(pdata,32);
2589 #ifdef LARGE_SMB_OFF_T
2590         qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2591 #else /* LARGE_SMB_OFF_T */
2592         if ((IVAL(pdata,36) != 0)&&
2593                 ((qt.hardlim != 0xFFFFFFFF)||
2594                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2595                 /* more than 32 bits? */
2596                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2597                 return;
2598         }
2599 #endif /* LARGE_SMB_OFF_T */
2600
2601         sid_parse(pdata+40,sid_len,&sid);
2602         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2603
2604         /* 44 unknown bytes left... */
2605
2606         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2607                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2608                 return;
2609         }
2610
2611         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2612                         pdata, data_len);
2613 }
2614 #endif /* HAVE_SYS_QUOTAS */
2615
2616 static void handle_nttrans(connection_struct *conn,
2617                            struct trans_state *state,
2618                            struct smb_request *req)
2619 {
2620         if (get_Protocol() >= PROTOCOL_NT1) {
2621                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2622                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2623         }
2624
2625
2626         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2627
2628         /* Now we must call the relevant NT_TRANS function */
2629         switch(state->call) {
2630                 case NT_TRANSACT_CREATE:
2631                 {
2632                         START_PROFILE(NT_transact_create);
2633                         call_nt_transact_create(
2634                                 conn, req,
2635                                 &state->setup, state->setup_count,
2636                                 &state->param, state->total_param,
2637                                 &state->data, state->total_data,
2638                                 state->max_data_return);
2639                         END_PROFILE(NT_transact_create);
2640                         break;
2641                 }
2642
2643                 case NT_TRANSACT_IOCTL:
2644                 {
2645                         START_PROFILE(NT_transact_ioctl);
2646                         call_nt_transact_ioctl(
2647                                 conn, req,
2648                                 &state->setup, state->setup_count,
2649                                 &state->param, state->total_param,
2650                                 &state->data, state->total_data,
2651                                 state->max_data_return);
2652                         END_PROFILE(NT_transact_ioctl);
2653                         break;
2654                 }
2655
2656                 case NT_TRANSACT_SET_SECURITY_DESC:
2657                 {
2658                         START_PROFILE(NT_transact_set_security_desc);
2659                         call_nt_transact_set_security_desc(
2660                                 conn, req,
2661                                 &state->setup, state->setup_count,
2662                                 &state->param, state->total_param,
2663                                 &state->data, state->total_data,
2664                                 state->max_data_return);
2665                         END_PROFILE(NT_transact_set_security_desc);
2666                         break;
2667                 }
2668
2669                 case NT_TRANSACT_NOTIFY_CHANGE:
2670                 {
2671                         START_PROFILE(NT_transact_notify_change);
2672                         call_nt_transact_notify_change(
2673                                 conn, req,
2674                                 &state->setup, state->setup_count,
2675                                 &state->param, state->total_param,
2676                                 &state->data, state->total_data,
2677                                 state->max_data_return,
2678                                 state->max_param_return);
2679                         END_PROFILE(NT_transact_notify_change);
2680                         break;
2681                 }
2682
2683                 case NT_TRANSACT_RENAME:
2684                 {
2685                         START_PROFILE(NT_transact_rename);
2686                         call_nt_transact_rename(
2687                                 conn, req,
2688                                 &state->setup, state->setup_count,
2689                                 &state->param, state->total_param,
2690                                 &state->data, state->total_data,
2691                                 state->max_data_return);
2692                         END_PROFILE(NT_transact_rename);
2693                         break;
2694                 }
2695
2696                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2697                 {
2698                         START_PROFILE(NT_transact_query_security_desc);
2699                         call_nt_transact_query_security_desc(
2700                                 conn, req,
2701                                 &state->setup, state->setup_count,
2702                                 &state->param, state->total_param,
2703                                 &state->data, state->total_data,
2704                                 state->max_data_return);
2705                         END_PROFILE(NT_transact_query_security_desc);
2706                         break;
2707                 }
2708
2709 #ifdef HAVE_SYS_QUOTAS
2710                 case NT_TRANSACT_GET_USER_QUOTA:
2711                 {
2712                         START_PROFILE(NT_transact_get_user_quota);
2713                         call_nt_transact_get_user_quota(
2714                                 conn, req,
2715                                 &state->setup, state->setup_count,
2716                                 &state->param, state->total_param,
2717                                 &state->data, state->total_data,
2718                                 state->max_data_return);
2719                         END_PROFILE(NT_transact_get_user_quota);
2720                         break;
2721                 }
2722
2723                 case NT_TRANSACT_SET_USER_QUOTA:
2724                 {
2725                         START_PROFILE(NT_transact_set_user_quota);
2726                         call_nt_transact_set_user_quota(
2727                                 conn, req,
2728                                 &state->setup, state->setup_count,
2729                                 &state->param, state->total_param,
2730                                 &state->data, state->total_data,
2731                                 state->max_data_return);
2732                         END_PROFILE(NT_transact_set_user_quota);
2733                         break;
2734                 }
2735 #endif /* HAVE_SYS_QUOTAS */
2736
2737                 default:
2738                         /* Error in request */
2739                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2740                                  "nttrans call\n", state->call));
2741                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2742                         return;
2743         }
2744         return;
2745 }
2746
2747 /****************************************************************************
2748  Reply to a SMBNTtrans.
2749 ****************************************************************************/
2750
2751 void reply_nttrans(struct smb_request *req)
2752 {
2753         connection_struct *conn = req->conn;
2754         uint32_t pscnt;
2755         uint32_t psoff;
2756         uint32_t dscnt;
2757         uint32_t dsoff;
2758         uint16 function_code;
2759         NTSTATUS result;
2760         struct trans_state *state;
2761
2762         START_PROFILE(SMBnttrans);
2763
2764         if (req->wct < 19) {
2765                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2766                 END_PROFILE(SMBnttrans);
2767                 return;
2768         }
2769
2770         pscnt = IVAL(req->vwv+9, 1);
2771         psoff = IVAL(req->vwv+11, 1);
2772         dscnt = IVAL(req->vwv+13, 1);
2773         dsoff = IVAL(req->vwv+15, 1);
2774         function_code = SVAL(req->vwv+18, 0);
2775
2776         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2777                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2778                 END_PROFILE(SMBnttrans);
2779                 return;
2780         }
2781
2782         result = allow_new_trans(conn->pending_trans, req->mid);
2783         if (!NT_STATUS_IS_OK(result)) {
2784                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2785                 reply_nterror(req, result);
2786                 END_PROFILE(SMBnttrans);
2787                 return;
2788         }
2789
2790         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2791                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2792                 END_PROFILE(SMBnttrans);
2793                 return;
2794         }
2795
2796         state->cmd = SMBnttrans;
2797
2798         state->mid = req->mid;
2799         state->vuid = req->vuid;
2800         state->total_data = IVAL(req->vwv+3, 1);
2801         state->data = NULL;
2802         state->total_param = IVAL(req->vwv+1, 1);
2803         state->param = NULL;
2804         state->max_data_return = IVAL(req->vwv+7, 1);
2805         state->max_param_return = IVAL(req->vwv+5, 1);
2806
2807         /* setup count is in *words* */
2808         state->setup_count = 2*CVAL(req->vwv+17, 1);
2809         state->setup = NULL;
2810         state->call = function_code;
2811
2812         DEBUG(10, ("num_setup=%u, "
2813                    "param_total=%u, this_param=%u, max_param=%u, "
2814                    "data_total=%u, this_data=%u, max_data=%u, "
2815                    "param_offset=%u, data_offset=%u\n",
2816                    (unsigned)state->setup_count,
2817                    (unsigned)state->total_param, (unsigned)pscnt,
2818                    (unsigned)state->max_param_return,
2819                    (unsigned)state->total_data, (unsigned)dscnt,
2820                    (unsigned)state->max_data_return,
2821                    (unsigned)psoff, (unsigned)dsoff));
2822
2823         /*
2824          * All nttrans messages we handle have smb_wct == 19 +
2825          * state->setup_count.  Ensure this is so as a sanity check.
2826          */
2827
2828         if(req->wct != 19 + (state->setup_count/2)) {
2829                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2830                          req->wct, 19 + (state->setup_count/2)));
2831                 goto bad_param;
2832         }
2833
2834         /* Don't allow more than 128mb for each value. */
2835         if ((state->total_data > (1024*1024*128)) ||
2836             (state->total_param > (1024*1024*128))) {
2837                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2838                 END_PROFILE(SMBnttrans);
2839                 return;
2840         }
2841
2842         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2843                 goto bad_param;
2844
2845         if (state->total_data)  {
2846
2847                 if (trans_oob(state->total_data, 0, dscnt)
2848                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2849                         goto bad_param;
2850                 }
2851
2852                 /* Can't use talloc here, the core routines do realloc on the
2853                  * params and data. */
2854                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2855                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2856                                  "bytes !\n", (unsigned int)state->total_data));
2857                         TALLOC_FREE(state);
2858                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2859                         END_PROFILE(SMBnttrans);
2860                         return;
2861                 }
2862
2863                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2864         }
2865
2866         if (state->total_param) {
2867
2868                 if (trans_oob(state->total_param, 0, pscnt)
2869                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2870                         goto bad_param;
2871                 }
2872
2873                 /* Can't use talloc here, the core routines do realloc on the
2874                  * params and data. */
2875                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2876                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2877                                  "bytes !\n", (unsigned int)state->total_param));
2878                         SAFE_FREE(state->data);
2879                         TALLOC_FREE(state);
2880                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2881                         END_PROFILE(SMBnttrans);
2882                         return;
2883                 }
2884
2885                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2886         }
2887
2888         state->received_data  = dscnt;
2889         state->received_param = pscnt;
2890
2891         if(state->setup_count > 0) {
2892                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2893                           state->setup_count));
2894
2895                 /*
2896                  * No overflow possible here, state->setup_count is an
2897                  * unsigned int, being filled by a single byte from
2898                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
2899                  * below is not necessary, it's here to clarify things. The
2900                  * validity of req->vwv and req->wct has been checked in
2901                  * init_smb_request already.
2902                  */
2903                 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2904                         goto bad_param;
2905                 }
2906
2907                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2908                 if (state->setup == NULL) {
2909                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2910                         SAFE_FREE(state->data);
2911                         SAFE_FREE(state->param);
2912                         TALLOC_FREE(state);
2913                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2914                         END_PROFILE(SMBnttrans);
2915                         return;
2916                 }
2917
2918                 memcpy(state->setup, req->vwv+19, state->setup_count);
2919                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2920         }
2921
2922         if ((state->received_data == state->total_data) &&
2923             (state->received_param == state->total_param)) {
2924                 handle_nttrans(conn, state, req);
2925                 SAFE_FREE(state->param);
2926                 SAFE_FREE(state->data);
2927                 TALLOC_FREE(state);
2928                 END_PROFILE(SMBnttrans);
2929                 return;
2930         }
2931
2932         DLIST_ADD(conn->pending_trans, state);
2933
2934         /* We need to send an interim response then receive the rest
2935            of the parameter/data bytes */
2936         reply_outbuf(req, 0, 0);
2937         show_msg((char *)req->outbuf);
2938         END_PROFILE(SMBnttrans);
2939         return;
2940
2941   bad_param:
2942
2943         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2944         SAFE_FREE(state->data);
2945         SAFE_FREE(state->param);
2946         TALLOC_FREE(state);
2947         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2948         END_PROFILE(SMBnttrans);
2949         return;
2950 }
2951
2952 /****************************************************************************
2953  Reply to a SMBnttranss
2954  ****************************************************************************/
2955
2956 void reply_nttranss(struct smb_request *req)
2957 {
2958         connection_struct *conn = req->conn;
2959         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2960         struct trans_state *state;
2961
2962         START_PROFILE(SMBnttranss);
2963
2964         show_msg((char *)req->inbuf);
2965
2966         if (req->wct < 18) {
2967                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2968                 END_PROFILE(SMBnttranss);
2969                 return;
2970         }
2971
2972         for (state = conn->pending_trans; state != NULL;
2973              state = state->next) {
2974                 if (state->mid == req->mid) {
2975                         break;
2976                 }
2977         }
2978
2979         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2980                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2981                 END_PROFILE(SMBnttranss);
2982                 return;
2983         }
2984
2985         /* Revise state->total_param and state->total_data in case they have
2986            changed downwards */
2987         if (IVAL(req->vwv+1, 1) < state->total_param) {
2988                 state->total_param = IVAL(req->vwv+1, 1);
2989         }
2990         if (IVAL(req->vwv+3, 1) < state->total_data) {
2991                 state->total_data = IVAL(req->vwv+3, 1);
2992         }
2993
2994         pcnt = IVAL(req->vwv+5, 1);
2995         poff = IVAL(req->vwv+7, 1);
2996         pdisp = IVAL(req->vwv+9, 1);
2997
2998         dcnt = IVAL(req->vwv+11, 1);
2999         doff = IVAL(req->vwv+13, 1);
3000         ddisp = IVAL(req->vwv+15, 1);
3001
3002         state->received_param += pcnt;
3003         state->received_data += dcnt;
3004
3005         if ((state->received_data > state->total_data) ||
3006             (state->received_param > state->total_param))
3007                 goto bad_param;
3008
3009         if (pcnt) {
3010                 if (trans_oob(state->total_param, pdisp, pcnt)
3011                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3012                         goto bad_param;
3013                 }
3014                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3015         }
3016
3017         if (dcnt) {
3018                 if (trans_oob(state->total_data, ddisp, dcnt)
3019                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3020                         goto bad_param;
3021                 }
3022                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3023         }
3024
3025         if ((state->received_param < state->total_param) ||
3026             (state->received_data < state->total_data)) {
3027                 END_PROFILE(SMBnttranss);
3028                 return;
3029         }
3030
3031         handle_nttrans(conn, state, req);
3032
3033         DLIST_REMOVE(conn->pending_trans, state);
3034         SAFE_FREE(state->data);
3035         SAFE_FREE(state->param);
3036         TALLOC_FREE(state);
3037         END_PROFILE(SMBnttranss);
3038         return;
3039
3040   bad_param:
3041
3042         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3043         DLIST_REMOVE(conn->pending_trans, state);
3044         SAFE_FREE(state->data);
3045         SAFE_FREE(state->param);
3046         TALLOC_FREE(state);
3047         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3048         END_PROFILE(SMBnttranss);
3049         return;
3050 }