f78eaf8ca537cff25d9510f336abdaf797d22bff
[sfrench/samba-autobuild/.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 = (uint8 *)&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         out->flags = 0;
158 }
159
160 /*
161  * Return the current share mode list for an open file.
162  * This uses similar (but simplified) logic to locking/locking.c
163  */
164
165 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
166                                 uint64_t dev,
167                                 uint64_t ino,
168                                 struct smb_share_mode_entry **pp_list,
169                                 unsigned char *p_delete_on_close)
170 {
171         TDB_DATA db_data;
172         struct smb_share_mode_entry *list = NULL;
173         int num_share_modes = 0;
174         struct locking_data *ld = NULL; /* internal samba db state. */
175         struct share_mode_entry *shares = NULL;
176         size_t i;
177         int list_num;
178
179         *pp_list = NULL;
180         *p_delete_on_close = 0;
181
182         db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
183         if (!db_data.dptr) {
184                 return 0;
185         }
186
187         ld = (struct locking_data *)db_data.dptr;
188         num_share_modes = ld->u.s.num_share_mode_entries;
189
190         if (!num_share_modes) {
191                 free(db_data.dptr);
192                 return 0;
193         }
194
195         list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
196         if (!list) {
197                 free(db_data.dptr);
198                 return -1;
199         }
200
201         memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
202
203         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
204
205         list_num = 0;
206         for (i = 0; i < num_share_modes; i++) {
207                 struct share_mode_entry *share = &shares[i];
208                 struct smb_share_mode_entry *sme = &list[list_num];
209                 struct process_id pid = share->pid;
210
211                 /* Check this process really exists. */
212                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
213                         continue; /* No longer exists. */
214                 }
215
216                 /* Ignore deferred open entries. */
217                 if (share->op_type == DEFERRED_OPEN_ENTRY) {
218                         continue;
219                 }
220
221                 /* Copy into the external list. */
222                 sme->dev = (uint64_t)share->dev;
223                 sme->ino = (uint64_t)share->inode;
224                 sme->share_access = (uint32_t)share->share_access;
225                 sme->access_mask = (uint32_t)share->access_mask;
226                 sme->open_time.tv_sec = share->time.tv_sec;
227                 sme->open_time.tv_usec = share->time.tv_usec;
228                 sme->file_id = (uint32_t)share->share_file_id;
229                 sme->pid = share->pid;
230                 list_num++;
231         }
232
233         if (list_num == 0) {
234                 free(db_data.dptr);
235                 free(list);
236                 return 0;
237         }
238
239         *p_delete_on_close = ld->u.s.delete_on_close;
240         *pp_list = list;
241         free(db_data.dptr);
242         return list_num;
243 }
244
245 /* 
246  * Create an entry in the Samba share mode db.
247  */
248
249 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
250                                 uint64_t dev,
251                                 uint64_t ino,
252                                 const struct smb_share_mode_entry *new_entry,
253                                 const char *sharepath, /* Must be absolute utf8 path. */
254                                 const char *filename) /* Must be relative utf8 path. */
255 {
256         TDB_DATA db_data;
257         TDB_DATA locking_key =  get_locking_key(dev, ino);
258         int orig_num_share_modes = 0;
259         struct locking_data *ld = NULL; /* internal samba db state. */
260         struct share_mode_entry *shares = NULL;
261         uint8 *new_data_p = NULL;
262         size_t new_data_size = 0;
263
264         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
265         if (!db_data.dptr) {
266                 /* We must create the entry. */
267                 db_data.dptr = (uint8 *)malloc(
268                         sizeof(struct locking_data) +
269                         sizeof(struct share_mode_entry) +
270                         strlen(sharepath) + 1 +
271                         strlen(filename) + 1);
272                 if (!db_data.dptr) {
273                         return -1;
274                 }
275                 ld = (struct locking_data *)db_data.dptr;
276                 memset(ld, '\0', sizeof(struct locking_data));
277                 ld->u.s.num_share_mode_entries = 1;
278                 ld->u.s.delete_on_close = 0;
279                 ld->u.s.delete_token_size = 0;
280                 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
281                 create_share_mode_entry(shares, new_entry);
282
283                 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
284                         sharepath,
285                         strlen(sharepath) + 1);
286                 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
287                         strlen(sharepath) + 1,
288                         filename,
289                         strlen(filename) + 1);
290
291                 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
292                                         strlen(sharepath) + 1 +
293                                         strlen(filename) + 1;
294                 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
295                         free(db_data.dptr);
296                         return -1;
297                 }
298                 free(db_data.dptr);
299                 return 0;
300         }
301
302         /* Entry exists, we must add a new entry. */
303         new_data_p = (uint8 *)malloc(
304                 db_data.dsize + sizeof(struct share_mode_entry));
305         if (!new_data_p) {
306                 free(db_data.dptr);
307                 return -1;
308         }
309
310         ld = (struct locking_data *)db_data.dptr;
311         orig_num_share_modes = ld->u.s.num_share_mode_entries;
312
313         /* Copy the original data. */
314         memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
315
316         /* Add in the new share mode */
317         shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
318                         (orig_num_share_modes * sizeof(struct share_mode_entry)));
319
320         create_share_mode_entry(shares, new_entry);
321
322         ld = (struct locking_data *)new_data_p;
323         ld->u.s.num_share_mode_entries++;
324
325         /* Append the original delete_token and filenames. */
326         memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
327                 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
328                 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
329
330         new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
331
332         free(db_data.dptr);
333
334         db_data.dptr = new_data_p;
335         db_data.dsize = new_data_size;
336
337         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
338                 free(db_data.dptr);
339                 return -1;
340         }
341         free(db_data.dptr);
342         return 0;
343 }
344
345 /* 
346  * Create an entry in the Samba share mode db. Original interface - doesn't
347  * Distinguish between share path and filename. Fudge this by using a
348  * sharepath of / and a relative filename of (filename+1).
349  */
350
351 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
352                                 uint64_t dev,
353                                 uint64_t ino,
354                                 const struct smb_share_mode_entry *new_entry,
355                                 const char *filename) /* Must be absolute utf8 path. */
356 {
357         if (*filename != '/') {
358                 abort();
359         }
360         return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
361                                                 "/", &filename[1]);
362 }
363
364 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
365                                 uint64_t dev,
366                                 uint64_t ino,
367                                 const struct smb_share_mode_entry *del_entry)
368 {
369         TDB_DATA db_data;
370         TDB_DATA locking_key =  get_locking_key(dev, ino);
371         int orig_num_share_modes = 0;
372         struct locking_data *ld = NULL; /* internal samba db state. */
373         struct share_mode_entry *shares = NULL;
374         uint8 *new_data_p = NULL;
375         size_t remaining_size = 0;
376         size_t i, num_share_modes;
377         const uint8 *remaining_ptr = NULL;
378
379         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
380         if (!db_data.dptr) {
381                 return -1; /* Error - missing entry ! */
382         }
383
384         ld = (struct locking_data *)db_data.dptr;
385         orig_num_share_modes = ld->u.s.num_share_mode_entries;
386         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
387
388         if (orig_num_share_modes == 1) {
389                 /* Only one entry - better be ours... */
390                 if (!share_mode_entry_equal(del_entry, shares)) {
391                         /* Error ! We can't delete someone else's entry ! */
392                         free(db_data.dptr);
393                         return -1;
394                 }
395                 /* It's ours - just remove the entire record. */
396                 free(db_data.dptr);
397                 return tdb_delete(db_ctx->smb_tdb, locking_key);
398         }
399
400         /* More than one - allocate a new record minus the one we'll delete. */
401         new_data_p = (uint8 *)malloc(
402                 db_data.dsize - sizeof(struct share_mode_entry));
403         if (!new_data_p) {
404                 free(db_data.dptr);
405                 return -1;
406         }
407
408         /* Copy the header. */
409         memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
410
411         num_share_modes = 0;
412         for (i = 0; i < orig_num_share_modes; i++) {
413                 struct share_mode_entry *share = &shares[i];
414                 struct process_id pid = share->pid;
415
416                 /* Check this process really exists. */
417                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
418                         continue; /* No longer exists. */
419                 }
420
421                 if (share_mode_entry_equal(del_entry, share)) {
422                         continue; /* This is our delete taget. */
423                 }
424
425                 memcpy(new_data_p + sizeof(struct locking_data) +
426                                 (num_share_modes * sizeof(struct share_mode_entry)),
427                         share, sizeof(struct share_mode_entry) );
428
429                 num_share_modes++;
430         }
431
432         if (num_share_modes == 0) {
433                 /* None left after pruning. Delete record. */
434                 free(db_data.dptr);
435                 free(new_data_p);
436                 return tdb_delete(db_ctx->smb_tdb, locking_key);
437         }
438
439         /* Copy any delete token plus the terminating filenames. */
440         remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
441         remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
442
443         memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
444                 remaining_ptr,
445                 remaining_size);
446
447         free(db_data.dptr);
448
449         db_data.dptr = new_data_p;
450
451         /* Re-save smaller record. */
452         ld = (struct locking_data *)db_data.dptr;
453         ld->u.s.num_share_mode_entries = num_share_modes;
454
455         db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
456
457         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
458                 free(db_data.dptr);
459                 return -1;
460         }
461         free(db_data.dptr);
462         return 0;
463 }
464
465 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
466                                 uint64_t dev,
467                                 uint64_t ino,
468                                 const struct smb_share_mode_entry *set_entry,
469                                 const struct smb_share_mode_entry *new_entry)
470 {
471         TDB_DATA db_data;
472         TDB_DATA locking_key =  get_locking_key(dev, ino);
473         int num_share_modes = 0;
474         struct locking_data *ld = NULL; /* internal samba db state. */
475         struct share_mode_entry *shares = NULL;
476         size_t i;
477         int found_entry = 0;
478
479         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
480         if (!db_data.dptr) {
481                 return -1; /* Error - missing entry ! */
482         }
483
484         ld = (struct locking_data *)db_data.dptr;
485         num_share_modes = ld->u.s.num_share_mode_entries;
486         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
487
488         for (i = 0; i < num_share_modes; i++) {
489                 struct share_mode_entry *share = &shares[i];
490                 struct process_id pid = share->pid;
491
492                 /* Check this process really exists. */
493                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
494                         continue; /* No longer exists. */
495                 }
496
497                 if (share_mode_entry_equal(set_entry, share)) {
498                         create_share_mode_entry(share, new_entry);
499                         found_entry = 1;
500                         break;
501                 }
502         }
503
504         if (!found_entry) {
505                 free(db_data.dptr);
506                 return -1;
507         }
508
509         /* Save modified data. */
510         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
511                 free(db_data.dptr);
512                 return -1;
513         }
514         free(db_data.dptr);
515         return 0;
516 }