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