s3:smbd: Add the ntstatus to the smb_panic in share_mode_lock_destructor
[samba.git] / source3 / locking / locking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Locking functions
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Jeremy Allison 1992-2006
6    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27    locking to deal with multiple share modes per open file.
28
29    September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30    support.
31
32    rewrtten completely to use new tdb code. Tridge, Dec '99
33
34    Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35    Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
36 */
37
38 #include "includes.h"
39
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_LOCKING
42
43 #define NO_LOCKING_COUNT (-1)
44
45 /* the locking database handle */
46 static struct db_context *lock_db;
47
48 /****************************************************************************
49  Debugging aids :-).
50 ****************************************************************************/
51
52 const char *lock_type_name(enum brl_type lock_type)
53 {
54         switch (lock_type) {
55                 case READ_LOCK:
56                         return "READ";
57                 case WRITE_LOCK:
58                         return "WRITE";
59                 case PENDING_READ_LOCK:
60                         return "PENDING_READ";
61                 case PENDING_WRITE_LOCK:
62                         return "PENDING_WRITE";
63                 default:
64                         return "other";
65         }
66 }
67
68 const char *lock_flav_name(enum brl_flavour lock_flav)
69 {
70         return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
71 }
72
73 /****************************************************************************
74  Utility function called to see if a file region is locked.
75  Called in the read/write codepath.
76 ****************************************************************************/
77
78 void init_strict_lock_struct(files_struct *fsp,
79                                 uint32 smbpid,
80                                 br_off start,
81                                 br_off size,
82                                 enum brl_type lock_type,
83                                 struct lock_struct *plock)
84 {
85         SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
86
87         plock->context.smbpid = smbpid;
88         plock->context.tid = fsp->conn->cnum;
89         plock->context.pid = procid_self();
90         plock->start = start;
91         plock->size = size;
92         plock->fnum = fsp->fnum;
93         plock->lock_type = lock_type;
94         plock->lock_flav = lp_posix_cifsu_locktype(fsp);
95 }
96
97 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
98 {
99         int strict_locking = lp_strict_locking(fsp->conn->params);
100         bool ret = False;
101
102         if (plock->size == 0) {
103                 return True;
104         }
105
106         if (!lp_locking(fsp->conn->params) || !strict_locking) {
107                 return True;
108         }
109
110         if (strict_locking == Auto) {
111                 if  (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
112                         DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
113                         ret = True;
114                 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
115                            (plock->lock_type == READ_LOCK)) {
116                         DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
117                         ret = True;
118                 } else {
119                         struct byte_range_lock *br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
120                         if (!br_lck) {
121                                 return True;
122                         }
123                         ret = brl_locktest(br_lck,
124                                         plock->context.smbpid,
125                                         plock->context.pid,
126                                         plock->start,
127                                         plock->size,
128                                         plock->lock_type,
129                                         plock->lock_flav);
130                         TALLOC_FREE(br_lck);
131                 }
132         } else {
133                 struct byte_range_lock *br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
134                 if (!br_lck) {
135                         return True;
136                 }
137                 ret = brl_locktest(br_lck,
138                                 plock->context.smbpid,
139                                 plock->context.pid,
140                                 plock->start,
141                                 plock->size,
142                                 plock->lock_type,
143                                 plock->lock_flav);
144                 TALLOC_FREE(br_lck);
145         }
146
147         DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
148                         "len=%.0f %s for fnum %d file %s\n",
149                         lock_flav_name(plock->lock_flav),
150                         (double)plock->start, (double)plock->size,
151                         ret ? "unlocked" : "locked",
152                         plock->fnum, fsp_str_dbg(fsp)));
153
154         return ret;
155 }
156
157 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
158 {
159 }
160
161 /****************************************************************************
162  Find out if a lock could be granted - return who is blocking us if we can't.
163 ****************************************************************************/
164
165 NTSTATUS query_lock(files_struct *fsp,
166                         uint32 *psmbpid,
167                         uint64_t *pcount,
168                         uint64_t *poffset,
169                         enum brl_type *plock_type,
170                         enum brl_flavour lock_flav)
171 {
172         struct byte_range_lock *br_lck = NULL;
173         NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
174
175         if (!fsp->can_lock) {
176                 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
177         }
178
179         if (!lp_locking(fsp->conn->params)) {
180                 return NT_STATUS_OK;
181         }
182
183         br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
184         if (!br_lck) {
185                 return NT_STATUS_NO_MEMORY;
186         }
187
188         status = brl_lockquery(br_lck,
189                         psmbpid,
190                         procid_self(),
191                         poffset,
192                         pcount,
193                         plock_type,
194                         lock_flav);
195
196         TALLOC_FREE(br_lck);
197         return status;
198 }
199
200 static void increment_current_lock_count(files_struct *fsp,
201     enum brl_flavour lock_flav)
202 {
203         if (lock_flav == WINDOWS_LOCK &&
204             fsp->current_lock_count != NO_LOCKING_COUNT) {
205                 /* blocking ie. pending, locks also count here,
206                  * as this is an efficiency counter to avoid checking
207                  * the lock db. on close. JRA. */
208
209                 fsp->current_lock_count++;
210         } else {
211                 /* Notice that this has had a POSIX lock request.
212                  * We can't count locks after this so forget them.
213                  */
214                 fsp->current_lock_count = NO_LOCKING_COUNT;
215         }
216 }
217
218 static void decrement_current_lock_count(files_struct *fsp,
219     enum brl_flavour lock_flav)
220 {
221         if (lock_flav == WINDOWS_LOCK &&
222             fsp->current_lock_count != NO_LOCKING_COUNT) {
223                 SMB_ASSERT(fsp->current_lock_count > 0);
224                 fsp->current_lock_count--;
225         }
226 }
227
228 /****************************************************************************
229  Utility function called by locking requests.
230 ****************************************************************************/
231
232 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
233                         files_struct *fsp,
234                         uint32 lock_pid,
235                         uint64_t count,
236                         uint64_t offset,
237                         enum brl_type lock_type,
238                         enum brl_flavour lock_flav,
239                         bool blocking_lock,
240                         NTSTATUS *perr,
241                         uint32 *plock_pid,
242                         struct blocking_lock_record *blr)
243 {
244         struct byte_range_lock *br_lck = NULL;
245
246         if (!fsp->can_lock) {
247                 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
248                 return NULL;
249         }
250
251         if (!lp_locking(fsp->conn->params)) {
252                 *perr = NT_STATUS_OK;
253                 return NULL;
254         }
255
256         /* NOTE! 0 byte long ranges ARE allowed and should be stored  */
257
258         DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
259                 "blocking_lock=%s requested for fnum %d file %s\n",
260                 lock_flav_name(lock_flav), lock_type_name(lock_type),
261                 (double)offset, (double)count, blocking_lock ? "true" :
262                 "false", fsp->fnum, fsp_str_dbg(fsp)));
263
264         br_lck = brl_get_locks(talloc_tos(), fsp);
265         if (!br_lck) {
266                 *perr = NT_STATUS_NO_MEMORY;
267                 return NULL;
268         }
269
270         *perr = brl_lock(msg_ctx,
271                         br_lck,
272                         lock_pid,
273                         procid_self(),
274                         offset,
275                         count, 
276                         lock_type,
277                         lock_flav,
278                         blocking_lock,
279                         plock_pid,
280                         blr);
281
282         DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
283
284         increment_current_lock_count(fsp, lock_flav);
285         return br_lck;
286 }
287
288 /****************************************************************************
289  Utility function called by unlocking requests.
290 ****************************************************************************/
291
292 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
293                         files_struct *fsp,
294                         uint32 lock_pid,
295                         uint64_t count,
296                         uint64_t offset,
297                         enum brl_flavour lock_flav)
298 {
299         bool ok = False;
300         struct byte_range_lock *br_lck = NULL;
301         
302         if (!fsp->can_lock) {
303                 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
304         }
305         
306         if (!lp_locking(fsp->conn->params)) {
307                 return NT_STATUS_OK;
308         }
309         
310         DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
311                   (double)offset, (double)count, fsp->fnum,
312                   fsp_str_dbg(fsp)));
313
314         br_lck = brl_get_locks(talloc_tos(), fsp);
315         if (!br_lck) {
316                 return NT_STATUS_NO_MEMORY;
317         }
318
319         ok = brl_unlock(msg_ctx,
320                         br_lck,
321                         lock_pid,
322                         procid_self(),
323                         offset,
324                         count,
325                         lock_flav);
326    
327         TALLOC_FREE(br_lck);
328
329         if (!ok) {
330                 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
331                 return NT_STATUS_RANGE_NOT_LOCKED;
332         }
333
334         decrement_current_lock_count(fsp, lock_flav);
335         return NT_STATUS_OK;
336 }
337
338 /****************************************************************************
339  Cancel any pending blocked locks.
340 ****************************************************************************/
341
342 NTSTATUS do_lock_cancel(files_struct *fsp,
343                         uint32 lock_pid,
344                         uint64_t count,
345                         uint64_t offset,
346                         enum brl_flavour lock_flav,
347                         struct blocking_lock_record *blr)
348 {
349         bool ok = False;
350         struct byte_range_lock *br_lck = NULL;
351
352         if (!fsp->can_lock) {
353                 return fsp->is_directory ?
354                         NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
355         }
356         
357         if (!lp_locking(fsp->conn->params)) {
358                 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
359         }
360
361         DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
362                   (double)offset, (double)count, fsp->fnum,
363                   fsp_str_dbg(fsp)));
364
365         br_lck = brl_get_locks(talloc_tos(), fsp);
366         if (!br_lck) {
367                 return NT_STATUS_NO_MEMORY;
368         }
369
370         ok = brl_lock_cancel(br_lck,
371                         lock_pid,
372                         procid_self(),
373                         offset,
374                         count,
375                         lock_flav,
376                         blr);
377
378         TALLOC_FREE(br_lck);
379
380         if (!ok) {
381                 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
382                 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
383         }
384
385         decrement_current_lock_count(fsp, lock_flav);
386         return NT_STATUS_OK;
387 }
388
389 /****************************************************************************
390  Remove any locks on this fd. Called from file_close().
391 ****************************************************************************/
392
393 void locking_close_file(struct messaging_context *msg_ctx,
394                         files_struct *fsp)
395 {
396         struct byte_range_lock *br_lck;
397
398         if (!lp_locking(fsp->conn->params)) {
399                 return;
400         }
401
402         /* If we have not outstanding locks or pending
403          * locks then we don't need to look in the lock db.
404          */
405
406         if (fsp->current_lock_count == 0) {
407                 return;
408         }
409
410         br_lck = brl_get_locks(talloc_tos(),fsp);
411
412         if (br_lck) {
413                 cancel_pending_lock_requests_by_fid(fsp, br_lck);
414                 brl_close_fnum(msg_ctx, br_lck);
415                 TALLOC_FREE(br_lck);
416         }
417 }
418
419 /****************************************************************************
420  Initialise the locking functions.
421 ****************************************************************************/
422
423 static bool locking_init_internal(bool read_only)
424 {
425         brl_init(read_only);
426
427         if (lock_db)
428                 return True;
429
430         lock_db = db_open(NULL, lock_path("locking.tdb"),
431                           lp_open_files_db_hash_size(),
432                           TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
433                           read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
434
435         if (!lock_db) {
436                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
437                 return False;
438         }
439
440         if (!posix_locking_init(read_only))
441                 return False;
442
443         return True;
444 }
445
446 bool locking_init(void)
447 {
448         return locking_init_internal(false);
449 }
450
451 bool locking_init_readonly(void)
452 {
453         return locking_init_internal(true);
454 }
455
456 /*******************************************************************
457  Deinitialize the share_mode management.
458 ******************************************************************/
459
460 bool locking_end(void)
461 {
462         brl_shutdown();
463         TALLOC_FREE(lock_db);
464         return true;
465 }
466
467 /*******************************************************************
468  Form a static locking key for a dev/inode pair.
469 ******************************************************************/
470
471 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
472 {
473         *tmp = *id;
474         return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
475 }
476
477 /*******************************************************************
478  Print out a share mode.
479 ********************************************************************/
480
481 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
482 {
483         return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
484                  "pid = %s, share_access = 0x%x, private_options = 0x%x, "
485                  "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
486                  "uid = %u, flags = %u, file_id %s",
487                  num,
488                  e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
489                  procid_str_static(&e->pid),
490                  e->share_access, e->private_options,
491                  e->access_mask, e->op_mid, e->op_type, e->share_file_id,
492                  (unsigned int)e->uid, (unsigned int)e->flags,
493                  file_id_string_tos(&e->id));
494 }
495
496 /*******************************************************************
497  Print out a share mode table.
498 ********************************************************************/
499
500 static void print_share_mode_table(struct locking_data *data)
501 {
502         int num_share_modes = data->u.s.num_share_mode_entries;
503         struct share_mode_entry *shares =
504                 (struct share_mode_entry *)(data + 1);
505         int i;
506
507         for (i = 0; i < num_share_modes; i++) {
508                 struct share_mode_entry entry;
509                 char *str;
510
511                 /*
512                  * We need to memcpy the entry here due to alignment
513                  * restrictions that are not met when directly accessing
514                  * shares[i]
515                  */
516
517                 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
518                 str = share_mode_str(talloc_tos(), i, &entry);
519
520                 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
521                 TALLOC_FREE(str);
522         }
523 }
524
525 /*******************************************************************
526  Get all share mode entries for a dev/inode pair.
527 ********************************************************************/
528
529 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
530 {
531         struct locking_data data;
532         int i;
533
534         if (dbuf.dsize < sizeof(struct locking_data)) {
535                 smb_panic("parse_share_modes: buffer too short");
536         }
537
538         memcpy(&data, dbuf.dptr, sizeof(data));
539
540         lck->delete_on_close = data.u.s.delete_on_close;
541         lck->old_write_time = data.u.s.old_write_time;
542         lck->changed_write_time = data.u.s.changed_write_time;
543         lck->num_share_modes = data.u.s.num_share_mode_entries;
544
545         DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
546                    "cwrt: %s, tok: %u, num_share_modes: %d\n",
547                    lck->delete_on_close,
548                    timestring(debug_ctx(),
549                               convert_timespec_to_time_t(lck->old_write_time)),
550                    timestring(debug_ctx(),
551                               convert_timespec_to_time_t(
552                                       lck->changed_write_time)),
553                    (unsigned int)data.u.s.delete_token_size,
554                    lck->num_share_modes));
555
556         if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
557                 DEBUG(0, ("invalid number of share modes: %d\n",
558                           lck->num_share_modes));
559                 smb_panic("parse_share_modes: invalid number of share modes");
560         }
561
562         lck->share_modes = NULL;
563         
564         if (lck->num_share_modes != 0) {
565
566                 if (dbuf.dsize < (sizeof(struct locking_data) +
567                                   (lck->num_share_modes *
568                                    sizeof(struct share_mode_entry)))) {
569                         smb_panic("parse_share_modes: buffer too short");
570                 }
571                                   
572                 lck->share_modes = (struct share_mode_entry *)
573                         TALLOC_MEMDUP(lck,
574                                       dbuf.dptr+sizeof(struct locking_data),
575                                       lck->num_share_modes *
576                                       sizeof(struct share_mode_entry));
577
578                 if (lck->share_modes == NULL) {
579                         smb_panic("parse_share_modes: talloc failed");
580                 }
581         }
582
583         /* Get any delete token. */
584         if (data.u.s.delete_token_size) {
585                 uint8 *p = dbuf.dptr + sizeof(struct locking_data) +
586                                 (lck->num_share_modes *
587                                 sizeof(struct share_mode_entry));
588
589                 if ((data.u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
590                                 ((data.u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
591                         DEBUG(0, ("parse_share_modes: invalid token size %d\n",
592                                 data.u.s.delete_token_size));
593                         smb_panic("parse_share_modes: invalid token size");
594                 }
595
596                 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
597                 if (!lck->delete_token) {
598                         smb_panic("parse_share_modes: talloc failed");
599                 }
600
601                 /* Copy out the uid and gid. */
602                 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
603                 p += sizeof(uid_t);
604                 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
605                 p += sizeof(gid_t);
606
607                 /* Any supplementary groups ? */
608                 lck->delete_token->ngroups = (data.u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
609                                         ((data.u.s.delete_token_size -
610                                                 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
611
612                 if (lck->delete_token->ngroups) {
613                         /* Make this a talloc child of lck->delete_token. */
614                         lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
615                                                         lck->delete_token->ngroups);
616                         if (!lck->delete_token) {
617                                 smb_panic("parse_share_modes: talloc failed");
618                         }
619
620                         for (i = 0; i < lck->delete_token->ngroups; i++) {
621                                 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
622                                 p += sizeof(gid_t);
623                         }
624                 }
625
626         } else {
627                 lck->delete_token = NULL;
628         }
629
630         /* Save off the associated service path and filename. */
631         lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
632                 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
633                 data.u.s.delete_token_size;
634
635         lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
636                 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
637                 data.u.s.delete_token_size +
638                 strlen(lck->servicepath) + 1;
639
640         lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
641                 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
642                 data.u.s.delete_token_size +
643                 strlen(lck->servicepath) + 1 +
644                 strlen(lck->base_name) + 1;
645
646         /*
647          * Ensure that each entry has a real process attached.
648          */
649
650         for (i = 0; i < lck->num_share_modes; i++) {
651                 struct share_mode_entry *entry_p = &lck->share_modes[i];
652                 char *str = NULL;
653                 if (DEBUGLEVEL >= 10) {
654                         str = share_mode_str(NULL, i, entry_p);
655                 }
656                 DEBUG(10,("parse_share_modes: %s\n",
657                         str ? str : ""));
658                 if (!process_exists(entry_p->pid)) {
659                         DEBUG(10,("parse_share_modes: deleted %s\n",
660                                 str ? str : ""));
661                         entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
662                         lck->modified = True;
663                 }
664                 TALLOC_FREE(str);
665         }
666
667         return True;
668 }
669
670 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
671 {
672         TDB_DATA result;
673         int num_valid = 0;
674         int i;
675         struct locking_data *data;
676         ssize_t offset;
677         ssize_t sp_len, bn_len, sn_len;
678         uint32 delete_token_size;
679
680         result.dptr = NULL;
681         result.dsize = 0;
682
683         for (i=0; i<lck->num_share_modes; i++) {
684                 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
685                         num_valid += 1;
686                 }
687         }
688
689         if (num_valid == 0) {
690                 return result;
691         }
692
693         sp_len = strlen(lck->servicepath);
694         bn_len = strlen(lck->base_name);
695         sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
696
697         delete_token_size = (lck->delete_token ?
698                         (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
699
700         result.dsize = sizeof(*data) +
701                 lck->num_share_modes * sizeof(struct share_mode_entry) +
702                 delete_token_size +
703                 sp_len + 1 +
704                 bn_len + 1 +
705                 sn_len + 1;
706         result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
707
708         if (result.dptr == NULL) {
709                 smb_panic("talloc failed");
710         }
711
712         data = (struct locking_data *)result.dptr;
713         ZERO_STRUCTP(data);
714         data->u.s.num_share_mode_entries = lck->num_share_modes;
715         data->u.s.delete_on_close = lck->delete_on_close;
716         data->u.s.old_write_time = lck->old_write_time;
717         data->u.s.changed_write_time = lck->changed_write_time;
718         data->u.s.delete_token_size = delete_token_size;
719
720         DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
721                   "num: %d\n", data->u.s.delete_on_close,
722                   timestring(debug_ctx(),
723                              convert_timespec_to_time_t(lck->old_write_time)),
724                   timestring(debug_ctx(),
725                              convert_timespec_to_time_t(
726                                      lck->changed_write_time)),
727                   (unsigned int)data->u.s.delete_token_size,
728                   data->u.s.num_share_mode_entries));
729
730         memcpy(result.dptr + sizeof(*data), lck->share_modes,
731                sizeof(struct share_mode_entry)*lck->num_share_modes);
732         offset = sizeof(*data) +
733                 sizeof(struct share_mode_entry)*lck->num_share_modes;
734
735         /* Store any delete on close token. */
736         if (lck->delete_token) {
737                 uint8 *p = result.dptr + offset;
738
739                 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
740                 p += sizeof(uid_t);
741
742                 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
743                 p += sizeof(gid_t);
744
745                 for (i = 0; i < lck->delete_token->ngroups; i++) {
746                         memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
747                         p += sizeof(gid_t);
748                 }
749                 offset = p - result.dptr;
750         }
751
752         safe_strcpy((char *)result.dptr + offset, lck->servicepath,
753                     result.dsize - offset - 1);
754         offset += sp_len + 1;
755         safe_strcpy((char *)result.dptr + offset, lck->base_name,
756                     result.dsize - offset - 1);
757         offset += bn_len + 1;
758         safe_strcpy((char *)result.dptr + offset, lck->stream_name,
759                     result.dsize - offset - 1);
760
761         if (DEBUGLEVEL >= 10) {
762                 print_share_mode_table(data);
763         }
764
765         return result;
766 }
767
768 static int share_mode_lock_destructor(struct share_mode_lock *lck)
769 {
770         NTSTATUS status;
771         TDB_DATA data;
772
773         if (!lck->modified) {
774                 return 0;
775         }
776
777         data = unparse_share_modes(lck);
778
779         if (data.dptr == NULL) {
780                 if (!lck->fresh) {
781                         /* There has been an entry before, delete it */
782
783                         status = lck->record->delete_rec(lck->record);
784                         if (!NT_STATUS_IS_OK(status)) {
785                                 char *errmsg;
786
787                                 DEBUG(0, ("delete_rec returned %s\n",
788                                           nt_errstr(status)));
789
790                                 if (asprintf(&errmsg, "could not delete share "
791                                              "entry: %s\n",
792                                              nt_errstr(status)) == -1) {
793                                         smb_panic("could not delete share"
794                                                   "entry");
795                                 }
796                                 smb_panic(errmsg);
797                         }
798                 }
799                 goto done;
800         }
801
802         status = lck->record->store(lck->record, data, TDB_REPLACE);
803         if (!NT_STATUS_IS_OK(status)) {
804                 char *errmsg;
805
806                 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
807
808                 if (asprintf(&errmsg, "could not store share mode entry: %s",
809                              nt_errstr(status)) == -1) {
810                         smb_panic("could not store share mode entry");
811                 }
812                 smb_panic(errmsg);
813         }
814
815  done:
816
817         return 0;
818 }
819
820 static bool fill_share_mode_lock(struct share_mode_lock *lck,
821                                  struct file_id id,
822                                  const char *servicepath,
823                                  const struct smb_filename *smb_fname,
824                                  TDB_DATA share_mode_data,
825                                  const struct timespec *old_write_time)
826 {
827         /* Ensure we set every field here as the destructor must be
828            valid even if parse_share_modes fails. */
829
830         lck->servicepath = NULL;
831         lck->base_name = NULL;
832         lck->stream_name = NULL;
833         lck->id = id;
834         lck->num_share_modes = 0;
835         lck->share_modes = NULL;
836         lck->delete_token = NULL;
837         lck->delete_on_close = False;
838         ZERO_STRUCT(lck->old_write_time);
839         ZERO_STRUCT(lck->changed_write_time);
840         lck->fresh = False;
841         lck->modified = False;
842
843         lck->fresh = (share_mode_data.dptr == NULL);
844
845         if (lck->fresh) {
846                 bool has_stream;
847                 if (smb_fname == NULL || servicepath == NULL
848                     || old_write_time == NULL) {
849                         return False;
850                 }
851
852                 has_stream = smb_fname->stream_name != NULL;
853
854                 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
855                 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
856                 lck->servicepath = talloc_strdup(lck, servicepath);
857                 if (lck->base_name == NULL ||
858                     (has_stream && lck->stream_name == NULL) ||
859                     lck->servicepath == NULL) {
860                         DEBUG(0, ("talloc failed\n"));
861                         return False;
862                 }
863                 lck->old_write_time = *old_write_time;
864         } else {
865                 if (!parse_share_modes(share_mode_data, lck)) {
866                         DEBUG(0, ("Could not parse share modes\n"));
867                         return False;
868                 }
869         }
870
871         return True;
872 }
873
874 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
875                                             const struct file_id id,
876                                             const char *servicepath,
877                                             const struct smb_filename *smb_fname,
878                                             const struct timespec *old_write_time)
879 {
880         struct share_mode_lock *lck;
881         struct file_id tmp;
882         TDB_DATA key = locking_key(&id, &tmp);
883
884         if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
885                 DEBUG(0, ("talloc failed\n"));
886                 return NULL;
887         }
888
889         if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
890                 DEBUG(3, ("Could not lock share entry\n"));
891                 TALLOC_FREE(lck);
892                 return NULL;
893         }
894
895         if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
896                                   lck->record->value, old_write_time)) {
897                 DEBUG(3, ("fill_share_mode_lock failed\n"));
898                 TALLOC_FREE(lck);
899                 return NULL;
900         }
901
902         talloc_set_destructor(lck, share_mode_lock_destructor);
903
904         return lck;
905 }
906
907 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
908                                                   const struct file_id id)
909 {
910         struct share_mode_lock *lck;
911         struct file_id tmp;
912         TDB_DATA key = locking_key(&id, &tmp);
913         TDB_DATA data;
914
915         if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
916                 DEBUG(0, ("talloc failed\n"));
917                 return NULL;
918         }
919
920         if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
921                 DEBUG(3, ("Could not fetch share entry\n"));
922                 TALLOC_FREE(lck);
923                 return NULL;
924         }
925
926         if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
927                 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
928                            "around (file not open)\n"));
929                 TALLOC_FREE(lck);
930                 return NULL;
931         }
932
933         return lck;
934 }
935
936 /*******************************************************************
937  Sets the service name and filename for rename.
938  At this point we emit "file renamed" messages to all
939  process id's that have this file open.
940  Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
941 ********************************************************************/
942
943 bool rename_share_filename(struct messaging_context *msg_ctx,
944                         struct share_mode_lock *lck,
945                         const char *servicepath,
946                         const struct smb_filename *smb_fname_dst)
947 {
948         size_t sp_len;
949         size_t bn_len;
950         size_t sn_len;
951         size_t msg_len;
952         char *frm = NULL;
953         int i;
954         bool strip_two_chars = false;
955         bool has_stream = smb_fname_dst->stream_name != NULL;
956
957         DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
958                    servicepath, smb_fname_dst->base_name));
959
960         /*
961          * rename_internal_fsp() and rename_internals() add './' to
962          * head of newname if newname does not contain a '/'.
963          */
964         if (smb_fname_dst->base_name[0] &&
965             smb_fname_dst->base_name[1] &&
966             smb_fname_dst->base_name[0] == '.' &&
967             smb_fname_dst->base_name[1] == '/') {
968                 strip_two_chars = true;
969         }
970
971         lck->servicepath = talloc_strdup(lck, servicepath);
972         lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
973                                        (strip_two_chars ? 2 : 0));
974         lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
975         if (lck->base_name == NULL ||
976             (has_stream && lck->stream_name == NULL) ||
977             lck->servicepath == NULL) {
978                 DEBUG(0, ("rename_share_filename: talloc failed\n"));
979                 return False;
980         }
981         lck->modified = True;
982
983         sp_len = strlen(lck->servicepath);
984         bn_len = strlen(lck->base_name);
985         sn_len = has_stream ? strlen(lck->stream_name) : 0;
986
987         msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
988             sn_len + 1;
989
990         /* Set up the name changed message. */
991         frm = TALLOC_ARRAY(lck, char, msg_len);
992         if (!frm) {
993                 return False;
994         }
995
996         push_file_id_24(frm, &lck->id);
997
998         DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
999
1000         safe_strcpy(&frm[24], lck->servicepath, sp_len);
1001         safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1002         safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1003                     sn_len);
1004
1005         /* Send the messages. */
1006         for (i=0; i<lck->num_share_modes; i++) {
1007                 struct share_mode_entry *se = &lck->share_modes[i];
1008                 if (!is_valid_share_mode_entry(se)) {
1009                         continue;
1010                 }
1011                 /* But not to ourselves... */
1012                 if (procid_is_me(&se->pid)) {
1013                         continue;
1014                 }
1015
1016                 DEBUG(10,("rename_share_filename: sending rename message to "
1017                           "pid %s file_id %s sharepath %s base_name %s "
1018                           "stream_name %s\n",
1019                           procid_str_static(&se->pid),
1020                           file_id_string_tos(&lck->id),
1021                           lck->servicepath, lck->base_name,
1022                         has_stream ? lck->stream_name : ""));
1023
1024                 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1025                                    (uint8 *)frm, msg_len);
1026         }
1027
1028         return True;
1029 }
1030
1031 void get_file_infos(struct file_id id,
1032                     bool *delete_on_close,
1033                     struct timespec *write_time)
1034 {
1035         struct share_mode_lock *lck;
1036
1037         if (delete_on_close) {
1038                 *delete_on_close = false;
1039         }
1040
1041         if (write_time) {
1042                 ZERO_STRUCTP(write_time);
1043         }
1044
1045         if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1046                 return;
1047         }
1048
1049         if (delete_on_close) {
1050                 *delete_on_close = lck->delete_on_close;
1051         }
1052
1053         if (write_time) {
1054                 struct timespec wt;
1055
1056                 wt = lck->changed_write_time;
1057                 if (null_timespec(wt)) {
1058                         wt = lck->old_write_time;
1059                 }
1060
1061                 *write_time = wt;
1062         }
1063
1064         TALLOC_FREE(lck);
1065 }
1066
1067 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1068 {
1069         int num_props = 0;
1070
1071         if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1072                 /* cope with dead entries from the process not
1073                    existing. These should not be considered valid,
1074                    otherwise we end up doing zero timeout sharing
1075                    violation */
1076                 return False;
1077         }
1078
1079         num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1080         num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1081         num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1082
1083         SMB_ASSERT(num_props <= 1);
1084         return (num_props != 0);
1085 }
1086
1087 bool is_deferred_open_entry(const struct share_mode_entry *e)
1088 {
1089         return (e->op_type == DEFERRED_OPEN_ENTRY);
1090 }
1091
1092 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1093 {
1094         return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1095 }
1096
1097 /*******************************************************************
1098  Fill a share mode entry.
1099 ********************************************************************/
1100
1101 static void fill_share_mode_entry(struct share_mode_entry *e,
1102                                   files_struct *fsp,
1103                                   uid_t uid, uint16 mid, uint16 op_type)
1104 {
1105         ZERO_STRUCTP(e);
1106         e->pid = procid_self();
1107         e->share_access = fsp->share_access;
1108         e->private_options = fsp->fh->private_options;
1109         e->access_mask = fsp->access_mask;
1110         e->op_mid = mid;
1111         e->op_type = op_type;
1112         e->time.tv_sec = fsp->open_time.tv_sec;
1113         e->time.tv_usec = fsp->open_time.tv_usec;
1114         e->id = fsp->file_id;
1115         e->share_file_id = fsp->fh->gen_id;
1116         e->uid = (uint32)uid;
1117         e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1118 }
1119
1120 static void fill_deferred_open_entry(struct share_mode_entry *e,
1121                                      const struct timeval request_time,
1122                                      struct file_id id, uint16 mid)
1123 {
1124         ZERO_STRUCTP(e);
1125         e->pid = procid_self();
1126         e->op_mid = mid;
1127         e->op_type = DEFERRED_OPEN_ENTRY;
1128         e->time.tv_sec = request_time.tv_sec;
1129         e->time.tv_usec = request_time.tv_usec;
1130         e->id = id;
1131         e->uid = (uint32)-1;
1132         e->flags = 0;
1133 }
1134
1135 static void add_share_mode_entry(struct share_mode_lock *lck,
1136                                  const struct share_mode_entry *entry)
1137 {
1138         int i;
1139
1140         for (i=0; i<lck->num_share_modes; i++) {
1141                 struct share_mode_entry *e = &lck->share_modes[i];
1142                 if (is_unused_share_mode_entry(e)) {
1143                         *e = *entry;
1144                         break;
1145                 }
1146         }
1147
1148         if (i == lck->num_share_modes) {
1149                 /* No unused entry found */
1150                 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1151                              &lck->share_modes, &lck->num_share_modes);
1152         }
1153         lck->modified = True;
1154 }
1155
1156 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1157                     uid_t uid, uint16 mid, uint16 op_type)
1158 {
1159         struct share_mode_entry entry;
1160         fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1161         add_share_mode_entry(lck, &entry);
1162 }
1163
1164 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
1165                        struct timeval request_time,
1166                        struct file_id id)
1167 {
1168         struct share_mode_entry entry;
1169         fill_deferred_open_entry(&entry, request_time, id, mid);
1170         add_share_mode_entry(lck, &entry);
1171 }
1172
1173 /*******************************************************************
1174  Check if two share mode entries are identical, ignoring oplock 
1175  and mid info and desired_access. (Removed paranoia test - it's
1176  not automatically a logic error if they are identical. JRA.)
1177 ********************************************************************/
1178
1179 static bool share_modes_identical(struct share_mode_entry *e1,
1180                                   struct share_mode_entry *e2)
1181 {
1182         /* We used to check for e1->share_access == e2->share_access here
1183            as well as the other fields but 2 different DOS or FCB opens
1184            sharing the same share mode entry may validly differ in
1185            fsp->share_access field. */
1186
1187         return (procid_equal(&e1->pid, &e2->pid) &&
1188                 file_id_equal(&e1->id, &e2->id) &&
1189                 e1->share_file_id == e2->share_file_id );
1190 }
1191
1192 static bool deferred_open_identical(struct share_mode_entry *e1,
1193                                     struct share_mode_entry *e2)
1194 {
1195         return (procid_equal(&e1->pid, &e2->pid) &&
1196                 (e1->op_mid == e2->op_mid) &&
1197                 file_id_equal(&e1->id, &e2->id));
1198 }
1199
1200 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1201                                                       struct share_mode_entry *entry)
1202 {
1203         int i;
1204
1205         for (i=0; i<lck->num_share_modes; i++) {
1206                 struct share_mode_entry *e = &lck->share_modes[i];
1207                 if (is_valid_share_mode_entry(entry) &&
1208                     is_valid_share_mode_entry(e) &&
1209                     share_modes_identical(e, entry)) {
1210                         return e;
1211                 }
1212                 if (is_deferred_open_entry(entry) &&
1213                     is_deferred_open_entry(e) &&
1214                     deferred_open_identical(e, entry)) {
1215                         return e;
1216                 }
1217         }
1218         return NULL;
1219 }
1220
1221 /*******************************************************************
1222  Del the share mode of a file for this process. Return the number of
1223  entries left.
1224 ********************************************************************/
1225
1226 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1227 {
1228         struct share_mode_entry entry, *e;
1229
1230         /* Don't care about the pid owner being correct here - just a search. */
1231         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1232
1233         e = find_share_mode_entry(lck, &entry);
1234         if (e == NULL) {
1235                 return False;
1236         }
1237
1238         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1239         lck->modified = True;
1240         return True;
1241 }
1242
1243 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1244 {
1245         struct share_mode_entry entry, *e;
1246
1247         fill_deferred_open_entry(&entry, timeval_zero(),
1248                                  lck->id, mid);
1249
1250         e = find_share_mode_entry(lck, &entry);
1251         if (e == NULL) {
1252                 return;
1253         }
1254
1255         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1256         lck->modified = True;
1257 }
1258
1259 /*******************************************************************
1260  Remove an oplock mid and mode entry from a share mode.
1261 ********************************************************************/
1262
1263 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1264 {
1265         struct share_mode_entry entry, *e;
1266
1267         /* Don't care about the pid owner being correct here - just a search. */
1268         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1269
1270         e = find_share_mode_entry(lck, &entry);
1271         if (e == NULL) {
1272                 return False;
1273         }
1274
1275         e->op_mid = 0;
1276         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1277                 /*
1278                  * Going from exclusive or batch,
1279                  * we always go through FAKE_LEVEL_II
1280                  * first.
1281                  */
1282                 e->op_type = FAKE_LEVEL_II_OPLOCK;
1283         } else {
1284                 e->op_type = NO_OPLOCK;
1285         }
1286         lck->modified = True;
1287         return True;
1288 }
1289
1290 /*******************************************************************
1291  Downgrade a oplock type from exclusive to level II.
1292 ********************************************************************/
1293
1294 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1295 {
1296         struct share_mode_entry entry, *e;
1297
1298         /* Don't care about the pid owner being correct here - just a search. */
1299         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1300
1301         e = find_share_mode_entry(lck, &entry);
1302         if (e == NULL) {
1303                 return False;
1304         }
1305
1306         e->op_type = LEVEL_II_OPLOCK;
1307         lck->modified = True;
1308         return True;
1309 }
1310
1311 /****************************************************************************
1312  Deal with the internal needs of setting the delete on close flag. Note that
1313  as the tdb locking is recursive, it is safe to call this from within 
1314  open_file_ntcreate. JRA.
1315 ****************************************************************************/
1316
1317 NTSTATUS can_set_delete_on_close(files_struct *fsp, bool delete_on_close,
1318                                  uint32 dosmode)
1319 {
1320         if (!delete_on_close) {
1321                 return NT_STATUS_OK;
1322         }
1323
1324         /*
1325          * Only allow delete on close for writable files.
1326          */
1327
1328         if ((dosmode & aRONLY) &&
1329             !lp_delete_readonly(SNUM(fsp->conn))) {
1330                 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1331                           "flag set but file attribute is readonly.\n",
1332                           fsp_str_dbg(fsp)));
1333                 return NT_STATUS_CANNOT_DELETE;
1334         }
1335
1336         /*
1337          * Only allow delete on close for writable shares.
1338          */
1339
1340         if (!CAN_WRITE(fsp->conn)) {
1341                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1342                           "close flag set but write access denied on share.\n",
1343                           fsp_str_dbg(fsp)));
1344                 return NT_STATUS_ACCESS_DENIED;
1345         }
1346
1347         /*
1348          * Only allow delete on close for files/directories opened with delete
1349          * intent.
1350          */
1351
1352         if (!(fsp->access_mask & DELETE_ACCESS)) {
1353                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1354                           "close flag set but delete access denied.\n",
1355                           fsp_str_dbg(fsp)));
1356                 return NT_STATUS_ACCESS_DENIED;
1357         }
1358
1359         /* Don't allow delete on close for non-empty directories. */
1360         if (fsp->is_directory) {
1361                 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1362                 return can_delete_directory(fsp->conn,
1363                                             fsp->fsp_name->base_name);
1364         }
1365
1366         return NT_STATUS_OK;
1367 }
1368
1369 /*************************************************************************
1370  Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1371  (Should this be in locking.c.... ?).
1372 *************************************************************************/
1373
1374 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, const UNIX_USER_TOKEN *tok)
1375 {
1376         UNIX_USER_TOKEN *cpy;
1377
1378         if (tok == NULL) {
1379                 return NULL;
1380         }
1381
1382         cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1383         if (!cpy) {
1384                 return NULL;
1385         }
1386
1387         cpy->uid = tok->uid;
1388         cpy->gid = tok->gid;
1389         cpy->ngroups = tok->ngroups;
1390         if (tok->ngroups) {
1391                 /* Make this a talloc child of cpy. */
1392                 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1393                 if (!cpy->groups) {
1394                         return NULL;
1395                 }
1396                 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1397         }
1398         return cpy;
1399 }
1400
1401 /****************************************************************************
1402  Replace the delete on close token.
1403 ****************************************************************************/
1404
1405 void set_delete_on_close_token(struct share_mode_lock *lck, const UNIX_USER_TOKEN *tok)
1406 {
1407         TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1408
1409         /* Copy the new token (can be NULL). */
1410         lck->delete_token = copy_unix_token(lck, tok);
1411         lck->modified = True;
1412 }
1413
1414 /****************************************************************************
1415  Sets the delete on close flag over all share modes on this file.
1416  Modify the share mode entry for all files open
1417  on this device and inode to tell other smbds we have
1418  changed the delete on close flag. This will be noticed
1419  in the close code, the last closer will delete the file
1420  if flag is set.
1421  This makes a copy of any UNIX_USER_TOKEN into the
1422  lck entry. This function is used when the lock is already granted.
1423 ****************************************************************************/
1424
1425 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1426 {
1427         if (lck->delete_on_close != delete_on_close) {
1428                 set_delete_on_close_token(lck, tok);
1429                 lck->delete_on_close = delete_on_close;
1430                 if (delete_on_close) {
1431                         SMB_ASSERT(lck->delete_token != NULL);
1432                 }
1433                 lck->modified = True;
1434         }
1435 }
1436
1437 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1438 {
1439         UNIX_USER_TOKEN *tok_copy = NULL;
1440         struct share_mode_lock *lck;
1441         
1442         DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1443                   "fnum = %d, file %s\n",
1444                   delete_on_close ? "Adding" : "Removing", fsp->fnum,
1445                   fsp_str_dbg(fsp)));
1446
1447         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1448                                   NULL);
1449         if (lck == NULL) {
1450                 return False;
1451         }
1452
1453         if (fsp->conn->admin_user) {
1454                 tok_copy = copy_unix_token(lck, tok);
1455                 if (tok_copy == NULL) {
1456                         TALLOC_FREE(lck);
1457                         return false;
1458                 }
1459                 tok_copy->uid = (uid_t)0;
1460                 tok = tok_copy;
1461         }
1462
1463         set_delete_on_close_lck(lck, delete_on_close, tok);
1464
1465         if (fsp->is_directory) {
1466                 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1467                 send_stat_cache_delete_message(fsp->fsp_name->base_name);
1468         }
1469
1470         TALLOC_FREE(lck);
1471         return True;
1472 }
1473
1474 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1475 {
1476         struct share_mode_lock *lck;
1477
1478         DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1479                  timestring(debug_ctx(),
1480                             convert_timespec_to_time_t(write_time)),
1481                  file_id_string_tos(&fileid)));
1482
1483         lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1484         if (lck == NULL) {
1485                 return False;
1486         }
1487
1488         if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1489                 lck->modified = True;
1490                 lck->changed_write_time = write_time;
1491         }
1492
1493         TALLOC_FREE(lck);
1494         return True;
1495 }
1496
1497 bool set_write_time(struct file_id fileid, struct timespec write_time)
1498 {
1499         struct share_mode_lock *lck;
1500
1501         DEBUG(5,("set_write_time: %s id=%s\n",
1502                  timestring(debug_ctx(),
1503                             convert_timespec_to_time_t(write_time)),
1504                  file_id_string_tos(&fileid)));
1505
1506         lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1507         if (lck == NULL) {
1508                 return False;
1509         }
1510
1511         if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1512                 lck->modified = True;
1513                 lck->old_write_time = write_time;
1514         }
1515
1516         TALLOC_FREE(lck);
1517         return True;
1518 }
1519
1520
1521 struct forall_state {
1522         void (*fn)(const struct share_mode_entry *entry,
1523                    const char *sharepath,
1524                    const char *fname,
1525                    void *private_data);
1526         void *private_data;
1527 };
1528
1529 static int traverse_fn(struct db_record *rec, void *_state)
1530 {
1531         struct forall_state *state = (struct forall_state *)_state;
1532         struct locking_data *data;
1533         struct share_mode_entry *shares;
1534         const char *sharepath;
1535         const char *fname;
1536         int i;
1537
1538         /* Ensure this is a locking_key record. */
1539         if (rec->key.dsize != sizeof(struct file_id))
1540                 return 0;
1541
1542         data = (struct locking_data *)rec->value.dptr;
1543         shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1544         sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1545                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1546                 data->u.s.delete_token_size;
1547         fname = (const char *)rec->value.dptr + sizeof(*data) +
1548                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1549                 data->u.s.delete_token_size +
1550                 strlen(sharepath) + 1;
1551
1552         for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1553                 state->fn(&shares[i], sharepath, fname,
1554                           state->private_data);
1555         }
1556         return 0;
1557 }
1558
1559 /*******************************************************************
1560  Call the specified function on each entry under management by the
1561  share mode system.
1562 ********************************************************************/
1563
1564 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1565                                  const char *, void *),
1566                       void *private_data)
1567 {
1568         struct forall_state state;
1569
1570         if (lock_db == NULL)
1571                 return 0;
1572
1573         state.fn = fn;
1574         state.private_data = private_data;
1575
1576         return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);
1577 }