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