r20883: W00t! I now understand how "delete on close" really
[samba.git] / source / libsmb / smb_share_modes.c
1 /*
2    Samba share mode database library external interface library.
3    Used by non-Samba products needing access to the Samba share mode db.
4                                                                                                                                   
5    Copyright (C) Jeremy Allison 2005 - 2006
6
7    sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
8
9      ** NOTE! The following LGPL license applies to this module only.
10      ** This does NOT imply that all of Samba is released
11      ** under the LGPL
12                                                                                                                                   
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 2 of the License, or (at your option) any later version.
17                                                                                                                                   
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22                                                                                                                                   
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 */
27
28 #include "includes.h"
29 #include "smb_share_modes.h"
30
31 /* Remove the paranoid malloc checker. */
32 #ifdef malloc
33 #undef malloc
34 #endif
35
36 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
37                                 uint64_t ino, const struct smb_share_mode_entry *new_entry,
38                                 const char *sharepath, const char *filename);
39
40 static BOOL sharemodes_procid_equal(const struct process_id *p1, const struct process_id *p2)
41 {
42         return (p1->pid == p2->pid);
43 }
44
45 static pid_t sharemodes_procid_to_pid(const struct process_id *proc)
46 {
47         return proc->pid;
48 }
49
50 /*
51  * open/close sharemode database.
52  */
53
54 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
55 {
56         struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
57
58         if (!smb_db) {
59                 return NULL;
60         }
61
62         memset(smb_db, '\0', sizeof(struct smbdb_ctx));
63
64         smb_db->smb_tdb = tdb_open(db_path,
65                                 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
66                                 O_RDWR|O_CREAT,
67                                 0644);
68
69         if (!smb_db->smb_tdb) {
70                 free(smb_db);
71                 return NULL;
72         }
73
74         /* Should check that this is the correct version.... */
75         return smb_db;
76 }
77
78 /* key and data records in the tdb locking database */
79 struct locking_key {
80         SMB_DEV_T dev;
81         SMB_INO_T inode;
82 };
83
84 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
85 {
86         int ret = tdb_close(db_ctx->smb_tdb);
87         free(db_ctx);
88         return ret;
89 }
90
91 static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
92 {
93         static struct locking_key lk;
94         TDB_DATA ld;
95
96         memset(&lk, '\0', sizeof(struct locking_key));
97         lk.dev = (SMB_DEV_T)dev;
98         lk.inode = (SMB_INO_T)ino;
99         ld.dptr = (char *)&lk;
100         ld.dsize = sizeof(lk);
101         return ld;
102 }
103
104 /*
105  * lock/unlock entry in sharemode database.
106  */
107
108 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
109                                 uint64_t dev,
110                                 uint64_t ino)
111 {
112         return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
113 }
114                                                                                                                                   
115 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
116                                 uint64_t dev,
117                                 uint64_t ino)
118 {
119         return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
120 }
121
122 /*
123  * Check if an external smb_share_mode_entry and an internal share_mode entry match.
124  */
125
126 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
127                                 const struct share_mode_entry *entry)
128 {
129         return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
130                 e_entry->file_id == (uint32_t)entry->share_file_id &&
131                 e_entry->open_time.tv_sec == entry->time.tv_sec &&
132                 e_entry->open_time.tv_usec == entry->time.tv_usec &&
133                 e_entry->share_access == (uint32_t)entry->share_access &&
134                 e_entry->access_mask == (uint32_t)entry->access_mask &&
135                 e_entry->dev == (uint64_t)entry->dev && 
136                 e_entry->ino == (uint64_t)entry->inode);
137 }
138
139 /*
140  * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
141  */
142
143 static void create_share_mode_entry(struct share_mode_entry *out,
144                                 const struct smb_share_mode_entry *in)
145 {
146         memset(out, '\0', sizeof(struct share_mode_entry));
147
148         out->pid = in->pid;
149         out->share_file_id = (unsigned long)in->file_id;
150         out->time.tv_sec = in->open_time.tv_sec;
151         out->time.tv_usec = in->open_time.tv_usec;
152         out->share_access = in->share_access;
153         out->access_mask = in->access_mask;
154         out->dev = (SMB_DEV_T)in->dev;
155         out->inode = (SMB_INO_T)in->ino;
156         out->uid = (uint32)geteuid();
157 }
158
159 /*
160  * Return the current share mode list for an open file.
161  * This uses similar (but simplified) logic to locking/locking.c
162  */
163
164 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
165                                 uint64_t dev,
166                                 uint64_t ino,
167                                 struct smb_share_mode_entry **pp_list,
168                                 unsigned char *p_delete_on_close)
169 {
170         TDB_DATA db_data;
171         struct smb_share_mode_entry *list = NULL;
172         int num_share_modes = 0;
173         struct locking_data *ld = NULL; /* internal samba db state. */
174         struct share_mode_entry *shares = NULL;
175         size_t i;
176         int list_num;
177
178         *pp_list = NULL;
179         *p_delete_on_close = 0;
180
181         db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
182         if (!db_data.dptr) {
183                 return 0;
184         }
185
186         ld = (struct locking_data *)db_data.dptr;
187         num_share_modes = ld->u.s.num_share_mode_entries;
188
189         if (!num_share_modes) {
190                 free(db_data.dptr);
191                 return 0;
192         }
193
194         list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
195         if (!list) {
196                 free(db_data.dptr);
197                 return -1;
198         }
199
200         memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
201
202         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
203
204         list_num = 0;
205         for (i = 0; i < num_share_modes; i++) {
206                 struct share_mode_entry *share = &shares[i];
207                 struct smb_share_mode_entry *sme = &list[list_num];
208                 struct process_id pid = share->pid;
209
210                 /* Check this process really exists. */
211                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
212                         continue; /* No longer exists. */
213                 }
214
215                 /* Ignore deferred open entries. */
216                 if (share->op_type == DEFERRED_OPEN_ENTRY) {
217                         continue;
218                 }
219
220                 /* Copy into the external list. */
221                 sme->dev = (uint64_t)share->dev;
222                 sme->ino = (uint64_t)share->inode;
223                 sme->share_access = (uint32_t)share->share_access;
224                 sme->access_mask = (uint32_t)share->access_mask;
225                 sme->open_time.tv_sec = share->time.tv_sec;
226                 sme->open_time.tv_usec = share->time.tv_usec;
227                 sme->file_id = (uint32_t)share->share_file_id;
228                 sme->pid = share->pid;
229                 list_num++;
230         }
231
232         if (list_num == 0) {
233                 free(db_data.dptr);
234                 free(list);
235                 return 0;
236         }
237
238         *p_delete_on_close = ld->u.s.delete_on_close;
239         *pp_list = list;
240         free(db_data.dptr);
241         return list_num;
242 }
243
244 /* 
245  * Create an entry in the Samba share mode db.
246  */
247
248 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
249                                 uint64_t dev,
250                                 uint64_t ino,
251                                 const struct smb_share_mode_entry *new_entry,
252                                 const char *sharepath, /* Must be absolute utf8 path. */
253                                 const char *filename) /* Must be relative utf8 path. */
254 {
255         TDB_DATA db_data;
256         TDB_DATA locking_key =  get_locking_key(dev, ino);
257         int orig_num_share_modes = 0;
258         struct locking_data *ld = NULL; /* internal samba db state. */
259         struct share_mode_entry *shares = NULL;
260         char *new_data_p = NULL;
261         size_t new_data_size = 0;
262
263         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
264         if (!db_data.dptr) {
265                 /* We must create the entry. */
266                 db_data.dptr = (char *)malloc(
267                         (2*sizeof(struct share_mode_entry)) +
268                         strlen(sharepath) + 1 +
269                         strlen(filename) + 1);
270                 if (!db_data.dptr) {
271                         return -1;
272                 }
273                 ld = (struct locking_data *)db_data.dptr;
274                 memset(ld, '\0', sizeof(struct locking_data));
275                 ld->u.s.num_share_mode_entries = 1;
276                 ld->u.s.delete_on_close = 0;
277                 ld->u.s.delete_token_size = 0;
278                 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
279                 create_share_mode_entry(shares, new_entry);
280
281                 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry),
282                         sharepath,
283                         strlen(sharepath) + 1);
284                 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry) +
285                         strlen(sharepath) + 1,
286                         filename,
287                         strlen(filename) + 1);
288
289                 db_data.dsize = 2*sizeof(struct share_mode_entry) +
290                                         strlen(sharepath) + 1 +
291                                         strlen(filename) + 1;
292                 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
293                         free(db_data.dptr);
294                         return -1;
295                 }
296                 free(db_data.dptr);
297                 return 0;
298         }
299
300         /* Entry exists, we must add a new entry. */
301         new_data_p = (char *)malloc(
302                 db_data.dsize + sizeof(struct share_mode_entry));
303         if (!new_data_p) {
304                 free(db_data.dptr);
305                 return -1;
306         }
307
308         ld = (struct locking_data *)db_data.dptr;
309         orig_num_share_modes = ld->u.s.num_share_mode_entries;
310
311         /* Copy the original data. */
312         memcpy(new_data_p, db_data.dptr, (orig_num_share_modes+1)*sizeof(struct share_mode_entry));
313
314         /* Add in the new share mode */
315         shares = (struct share_mode_entry *)(new_data_p +
316                         ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)));
317
318         create_share_mode_entry(shares, new_entry);
319
320         ld = (struct locking_data *)new_data_p;
321         ld->u.s.num_share_mode_entries++;
322
323         /* Append the original delete_token and filenames. */
324         memcpy(new_data_p + ((ld->u.s.num_share_mode_entries+1)*sizeof(struct share_mode_entry)),
325                 db_data.dptr + ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)),
326                 db_data.dsize - ((orig_num_share_modes+1) * sizeof(struct share_mode_entry)));
327
328         new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
329
330         free(db_data.dptr);
331
332         db_data.dptr = new_data_p;
333         db_data.dsize = new_data_size;
334
335         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
336                 free(db_data.dptr);
337                 return -1;
338         }
339         free(db_data.dptr);
340         return 0;
341 }
342
343 /* 
344  * Create an entry in the Samba share mode db. Original interface - doesn't
345  * Distinguish between share path and filename. Fudge this by using a
346  * sharepath of / and a relative filename of (filename+1).
347  */
348
349 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
350                                 uint64_t dev,
351                                 uint64_t ino,
352                                 const struct smb_share_mode_entry *new_entry,
353                                 const char *filename) /* Must be absolute utf8 path. */
354 {
355         if (*filename != '/') {
356                 abort();
357         }
358         return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
359                                                 "/", &filename[1]);
360 }
361
362 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
363                                 uint64_t dev,
364                                 uint64_t ino,
365                                 const struct smb_share_mode_entry *del_entry)
366 {
367         TDB_DATA db_data;
368         TDB_DATA locking_key =  get_locking_key(dev, ino);
369         int orig_num_share_modes = 0;
370         struct locking_data *ld = NULL; /* internal samba db state. */
371         struct share_mode_entry *shares = NULL;
372         char *new_data_p = NULL;
373         size_t remaining_size = 0;
374         size_t i, num_share_modes;
375         const char *remaining_ptr = NULL;
376
377         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
378         if (!db_data.dptr) {
379                 return -1; /* Error - missing entry ! */
380         }
381
382         ld = (struct locking_data *)db_data.dptr;
383         orig_num_share_modes = ld->u.s.num_share_mode_entries;
384         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
385
386         if (orig_num_share_modes == 1) {
387                 /* Only one entry - better be ours... */
388                 if (!share_mode_entry_equal(del_entry, shares)) {
389                         /* Error ! We can't delete someone else's entry ! */
390                         free(db_data.dptr);
391                         return -1;
392                 }
393                 /* It's ours - just remove the entire record. */
394                 free(db_data.dptr);
395                 return tdb_delete(db_ctx->smb_tdb, locking_key);
396         }
397
398         /* More than one - allocate a new record minus the one we'll delete. */
399         new_data_p = (char *)malloc(
400                 db_data.dsize - sizeof(struct share_mode_entry));
401         if (!new_data_p) {
402                 free(db_data.dptr);
403                 return -1;
404         }
405
406         /* Copy the header. */
407         memcpy(new_data_p, db_data.dptr, sizeof(struct share_mode_entry));
408
409         num_share_modes = 0;
410         for (i = 0; i < orig_num_share_modes; i++) {
411                 struct share_mode_entry *share = &shares[i];
412                 struct process_id pid = share->pid;
413
414                 /* Check this process really exists. */
415                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
416                         continue; /* No longer exists. */
417                 }
418
419                 if (share_mode_entry_equal(del_entry, share)) {
420                         continue; /* This is our delete taget. */
421                 }
422
423                 memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
424                         share, sizeof(struct share_mode_entry) );
425
426                 num_share_modes++;
427         }
428
429         if (num_share_modes == 0) {
430                 /* None left after pruning. Delete record. */
431                 free(db_data.dptr);
432                 free(new_data_p);
433                 return tdb_delete(db_ctx->smb_tdb, locking_key);
434         }
435
436         /* Copy any delete token plus the terminating filenames. */
437         remaining_ptr = db_data.dptr + ((orig_num_share_modes+1) * sizeof(struct share_mode_entry));
438         remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
439
440         memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
441                 remaining_ptr,
442                 remaining_size);
443
444         free(db_data.dptr);
445
446         db_data.dptr = new_data_p;
447
448         /* Re-save smaller record. */
449         ld = (struct locking_data *)db_data.dptr;
450         ld->u.s.num_share_mode_entries = num_share_modes;
451
452         db_data.dsize = ((num_share_modes+1)*sizeof(struct share_mode_entry)) + remaining_size;
453
454         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
455                 free(db_data.dptr);
456                 return -1;
457         }
458         free(db_data.dptr);
459         return 0;
460 }
461
462 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
463                                 uint64_t dev,
464                                 uint64_t ino,
465                                 const struct smb_share_mode_entry *set_entry,
466                                 const struct smb_share_mode_entry *new_entry)
467 {
468         TDB_DATA db_data;
469         TDB_DATA locking_key =  get_locking_key(dev, ino);
470         int num_share_modes = 0;
471         struct locking_data *ld = NULL; /* internal samba db state. */
472         struct share_mode_entry *shares = NULL;
473         size_t i;
474         int found_entry = 0;
475
476         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
477         if (!db_data.dptr) {
478                 return -1; /* Error - missing entry ! */
479         }
480
481         ld = (struct locking_data *)db_data.dptr;
482         num_share_modes = ld->u.s.num_share_mode_entries;
483         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
484
485         for (i = 0; i < num_share_modes; i++) {
486                 struct share_mode_entry *share = &shares[i];
487                 struct process_id pid = share->pid;
488
489                 /* Check this process really exists. */
490                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
491                         continue; /* No longer exists. */
492                 }
493
494                 if (share_mode_entry_equal(set_entry, share)) {
495                         create_share_mode_entry(share, new_entry);
496                         found_entry = 1;
497                         break;
498                 }
499         }
500
501         if (!found_entry) {
502                 free(db_data.dptr);
503                 return -1;
504         }
505
506         /* Save modified data. */
507         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
508                 free(db_data.dptr);
509                 return -1;
510         }
511         free(db_data.dptr);
512         return 0;
513 }