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