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