libsmb/smb_share_modes.c

説明を見る。
00001 /*
00002    Samba share mode database library external interface library.
00003    Used by non-Samba products needing access to the Samba share mode db.
00004                                                                                                                                   
00005    Copyright (C) Jeremy Allison 2005 - 2006
00006 
00007    sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
00008 
00009      ** NOTE! The following LGPL license applies to this module only.
00010      ** This does NOT imply that all of Samba is released
00011      ** under the LGPL
00012                                                                                                                                   
00013    This library is free software; you can redistribute it and/or
00014    modify it under the terms of the GNU Lesser General Public
00015    License as published by the Free Software Foundation; either
00016    version 2 of the License, or (at your option) any later version.
00017                                                                                                                                   
00018    This library is distributed in the hope that it will be useful,
00019    but WITHOUT ANY WARRANTY; without even the implied warranty of
00020    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021    Lesser General Public License for more details.
00022                                                                                                                                   
00023    You should have received a copy of the GNU Lesser General Public
00024    License along with this library; if not, write to the Free Software
00025    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 */
00027 
00028 #include "includes.h"
00029 #include "smb_share_modes.h"
00030 
00031 /* Database context handle. */
00032 struct smbdb_ctx {
00033         TDB_CONTEXT *smb_tdb;
00034 };
00035 
00036 /* Remove the paranoid malloc checker. */
00037 #ifdef malloc
00038 #undef malloc
00039 #endif
00040 
00041 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
00042                                 uint64_t ino, const struct smb_share_mode_entry *new_entry,
00043                                 const char *sharepath, const char *filename);
00044 
00045 static BOOL sharemodes_procid_equal(const struct process_id *p1, const struct process_id *p2)
00046 {
00047         return (p1->pid == p2->pid);
00048 }
00049 
00050 static pid_t sharemodes_procid_to_pid(const struct process_id *proc)
00051 {
00052         return proc->pid;
00053 }
00054 
00055 /*
00056  * open/close sharemode database.
00057  */
00058 
00059 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
00060 {
00061         struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
00062 
00063         if (!smb_db) {
00064                 return NULL;
00065         }
00066 
00067         memset(smb_db, '\0', sizeof(struct smbdb_ctx));
00068 
00069         smb_db->smb_tdb = tdb_open(db_path,
00070                                 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
00071                                 O_RDWR|O_CREAT,
00072                                 0644);
00073 
00074         if (!smb_db->smb_tdb) {
00075                 free(smb_db);
00076                 return NULL;
00077         }
00078 
00079         /* Should check that this is the correct version.... */
00080         return smb_db;
00081 }
00082 
00083 /* key and data records in the tdb locking database */
00084 struct locking_key {
00085         SMB_DEV_T dev;
00086         SMB_INO_T inode;
00087 };
00088 
00089 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
00090 {
00091         int ret = tdb_close(db_ctx->smb_tdb);
00092         free(db_ctx);
00093         return ret;
00094 }
00095 
00096 static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
00097 {
00098         static struct locking_key lk;
00099         TDB_DATA ld;
00100 
00101         memset(&lk, '\0', sizeof(struct locking_key));
00102         lk.dev = (SMB_DEV_T)dev;
00103         lk.inode = (SMB_INO_T)ino;
00104         ld.dptr = (char *)&lk;
00105         ld.dsize = sizeof(lk);
00106         return ld;
00107 }
00108 
00109 /*
00110  * lock/unlock entry in sharemode database.
00111  */
00112 
00113 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
00114                                 uint64_t dev,
00115                                 uint64_t ino)
00116 {
00117         return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
00118 }
00119                                                                                                                                   
00120 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
00121                                 uint64_t dev,
00122                                 uint64_t ino)
00123 {
00124         return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
00125 }
00126 
00127 /*
00128  * Check if an external smb_share_mode_entry and an internal share_mode entry match.
00129  */
00130 
00131 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
00132                                 const struct share_mode_entry *entry)
00133 {
00134         return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
00135                 e_entry->file_id == (uint32_t)entry->share_file_id &&
00136                 e_entry->open_time.tv_sec == entry->time.tv_sec &&
00137                 e_entry->open_time.tv_usec == entry->time.tv_usec &&
00138                 e_entry->share_access == (uint32_t)entry->share_access &&
00139                 e_entry->access_mask == (uint32_t)entry->access_mask &&
00140                 e_entry->dev == (uint64_t)entry->dev && 
00141                 e_entry->ino == (uint64_t)entry->inode);
00142 }
00143 
00144 /*
00145  * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
00146  */
00147 
00148 static void create_share_mode_entry(struct share_mode_entry *out,
00149                                 const struct smb_share_mode_entry *in)
00150 {
00151         memset(out, '\0', sizeof(struct share_mode_entry));
00152 
00153         out->pid = in->pid;
00154         out->share_file_id = (unsigned long)in->file_id;
00155         out->time.tv_sec = in->open_time.tv_sec;
00156         out->time.tv_usec = in->open_time.tv_usec;
00157         out->share_access = in->share_access;
00158         out->access_mask = in->access_mask;
00159         out->dev = (SMB_DEV_T)in->dev;
00160         out->inode = (SMB_INO_T)in->ino;
00161         out->uid = (uint32)geteuid();
00162         out->flags = 0;
00163 }
00164 
00165 /*
00166  * Return the current share mode list for an open file.
00167  * This uses similar (but simplified) logic to locking/locking.c
00168  */
00169 
00170 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
00171                                 uint64_t dev,
00172                                 uint64_t ino,
00173                                 struct smb_share_mode_entry **pp_list,
00174                                 unsigned char *p_delete_on_close)
00175 {
00176         TDB_DATA db_data;
00177         struct smb_share_mode_entry *list = NULL;
00178         int num_share_modes = 0;
00179         struct locking_data *ld = NULL; /* internal samba db state. */
00180         struct share_mode_entry *shares = NULL;
00181         size_t i;
00182         int list_num;
00183 
00184         *pp_list = NULL;
00185         *p_delete_on_close = 0;
00186 
00187         db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
00188         if (!db_data.dptr) {
00189                 return 0;
00190         }
00191 
00192         ld = (struct locking_data *)db_data.dptr;
00193         num_share_modes = ld->u.s.num_share_mode_entries;
00194 
00195         if (!num_share_modes) {
00196                 free(db_data.dptr);
00197                 return 0;
00198         }
00199 
00200         list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
00201         if (!list) {
00202                 free(db_data.dptr);
00203                 return -1;
00204         }
00205 
00206         memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
00207 
00208         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
00209 
00210         list_num = 0;
00211         for (i = 0; i < num_share_modes; i++) {
00212                 struct share_mode_entry *share = &shares[i];
00213                 struct smb_share_mode_entry *sme = &list[list_num];
00214                 struct process_id pid = share->pid;
00215 
00216                 /* Check this process really exists. */
00217                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
00218                         continue; /* No longer exists. */
00219                 }
00220 
00221                 /* Ignore deferred open entries. */
00222                 if (share->op_type == DEFERRED_OPEN_ENTRY) {
00223                         continue;
00224                 }
00225 
00226                 /* Copy into the external list. */
00227                 sme->dev = (uint64_t)share->dev;
00228                 sme->ino = (uint64_t)share->inode;
00229                 sme->share_access = (uint32_t)share->share_access;
00230                 sme->access_mask = (uint32_t)share->access_mask;
00231                 sme->open_time.tv_sec = share->time.tv_sec;
00232                 sme->open_time.tv_usec = share->time.tv_usec;
00233                 sme->file_id = (uint32_t)share->share_file_id;
00234                 sme->pid = share->pid;
00235                 list_num++;
00236         }
00237 
00238         if (list_num == 0) {
00239                 free(db_data.dptr);
00240                 free(list);
00241                 return 0;
00242         }
00243 
00244         *p_delete_on_close = ld->u.s.delete_on_close;
00245         *pp_list = list;
00246         free(db_data.dptr);
00247         return list_num;
00248 }
00249 
00250 /* 
00251  * Create an entry in the Samba share mode db.
00252  */
00253 
00254 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
00255                                 uint64_t dev,
00256                                 uint64_t ino,
00257                                 const struct smb_share_mode_entry *new_entry,
00258                                 const char *sharepath, /* Must be absolute utf8 path. */
00259                                 const char *filename) /* Must be relative utf8 path. */
00260 {
00261         TDB_DATA db_data;
00262         TDB_DATA locking_key =  get_locking_key(dev, ino);
00263         int orig_num_share_modes = 0;
00264         struct locking_data *ld = NULL; /* internal samba db state. */
00265         struct share_mode_entry *shares = NULL;
00266         char *new_data_p = NULL;
00267         size_t new_data_size = 0;
00268 
00269         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
00270         if (!db_data.dptr) {
00271                 /* We must create the entry. */
00272                 db_data.dptr = (char *)malloc(
00273                         sizeof(struct locking_data) +
00274                         sizeof(struct share_mode_entry) +
00275                         strlen(sharepath) + 1 +
00276                         strlen(filename) + 1);
00277                 if (!db_data.dptr) {
00278                         return -1;
00279                 }
00280                 ld = (struct locking_data *)db_data.dptr;
00281                 memset(ld, '\0', sizeof(struct locking_data));
00282                 ld->u.s.num_share_mode_entries = 1;
00283                 ld->u.s.delete_on_close = 0;
00284                 ld->u.s.delete_token_size = 0;
00285                 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
00286                 create_share_mode_entry(shares, new_entry);
00287 
00288                 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
00289                         sharepath,
00290                         strlen(sharepath) + 1);
00291                 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
00292                         strlen(sharepath) + 1,
00293                         filename,
00294                         strlen(filename) + 1);
00295 
00296                 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
00297                                         strlen(sharepath) + 1 +
00298                                         strlen(filename) + 1;
00299                 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
00300                         free(db_data.dptr);
00301                         return -1;
00302                 }
00303                 free(db_data.dptr);
00304                 return 0;
00305         }
00306 
00307         /* Entry exists, we must add a new entry. */
00308         new_data_p = (char *)malloc(
00309                 db_data.dsize + sizeof(struct share_mode_entry));
00310         if (!new_data_p) {
00311                 free(db_data.dptr);
00312                 return -1;
00313         }
00314 
00315         ld = (struct locking_data *)db_data.dptr;
00316         orig_num_share_modes = ld->u.s.num_share_mode_entries;
00317 
00318         /* Copy the original data. */
00319         memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
00320 
00321         /* Add in the new share mode */
00322         shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
00323                         (orig_num_share_modes * sizeof(struct share_mode_entry)));
00324 
00325         create_share_mode_entry(shares, new_entry);
00326 
00327         ld = (struct locking_data *)new_data_p;
00328         ld->u.s.num_share_mode_entries++;
00329 
00330         /* Append the original delete_token and filenames. */
00331         memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
00332                 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
00333                 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
00334 
00335         new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
00336 
00337         free(db_data.dptr);
00338 
00339         db_data.dptr = new_data_p;
00340         db_data.dsize = new_data_size;
00341 
00342         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
00343                 free(db_data.dptr);
00344                 return -1;
00345         }
00346         free(db_data.dptr);
00347         return 0;
00348 }
00349 
00350 /* 
00351  * Create an entry in the Samba share mode db. Original interface - doesn't
00352  * Distinguish between share path and filename. Fudge this by using a
00353  * sharepath of / and a relative filename of (filename+1).
00354  */
00355 
00356 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
00357                                 uint64_t dev,
00358                                 uint64_t ino,
00359                                 const struct smb_share_mode_entry *new_entry,
00360                                 const char *filename) /* Must be absolute utf8 path. */
00361 {
00362         if (*filename != '/') {
00363                 abort();
00364         }
00365         return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
00366                                                 "/", &filename[1]);
00367 }
00368 
00369 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
00370                                 uint64_t dev,
00371                                 uint64_t ino,
00372                                 const struct smb_share_mode_entry *del_entry)
00373 {
00374         TDB_DATA db_data;
00375         TDB_DATA locking_key =  get_locking_key(dev, ino);
00376         int orig_num_share_modes = 0;
00377         struct locking_data *ld = NULL; /* internal samba db state. */
00378         struct share_mode_entry *shares = NULL;
00379         char *new_data_p = NULL;
00380         size_t remaining_size = 0;
00381         size_t i, num_share_modes;
00382         const char *remaining_ptr = NULL;
00383 
00384         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
00385         if (!db_data.dptr) {
00386                 return -1; /* Error - missing entry ! */
00387         }
00388 
00389         ld = (struct locking_data *)db_data.dptr;
00390         orig_num_share_modes = ld->u.s.num_share_mode_entries;
00391         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
00392 
00393         if (orig_num_share_modes == 1) {
00394                 /* Only one entry - better be ours... */
00395                 if (!share_mode_entry_equal(del_entry, shares)) {
00396                         /* Error ! We can't delete someone else's entry ! */
00397                         free(db_data.dptr);
00398                         return -1;
00399                 }
00400                 /* It's ours - just remove the entire record. */
00401                 free(db_data.dptr);
00402                 return tdb_delete(db_ctx->smb_tdb, locking_key);
00403         }
00404 
00405         /* More than one - allocate a new record minus the one we'll delete. */
00406         new_data_p = (char *)malloc(
00407                 db_data.dsize - sizeof(struct share_mode_entry));
00408         if (!new_data_p) {
00409                 free(db_data.dptr);
00410                 return -1;
00411         }
00412 
00413         /* Copy the header. */
00414         memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
00415 
00416         num_share_modes = 0;
00417         for (i = 0; i < orig_num_share_modes; i++) {
00418                 struct share_mode_entry *share = &shares[i];
00419                 struct process_id pid = share->pid;
00420 
00421                 /* Check this process really exists. */
00422                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
00423                         continue; /* No longer exists. */
00424                 }
00425 
00426                 if (share_mode_entry_equal(del_entry, share)) {
00427                         continue; /* This is our delete taget. */
00428                 }
00429 
00430                 memcpy(new_data_p + sizeof(struct locking_data) +
00431                                 (num_share_modes * sizeof(struct share_mode_entry)),
00432                         share, sizeof(struct share_mode_entry) );
00433 
00434                 num_share_modes++;
00435         }
00436 
00437         if (num_share_modes == 0) {
00438                 /* None left after pruning. Delete record. */
00439                 free(db_data.dptr);
00440                 free(new_data_p);
00441                 return tdb_delete(db_ctx->smb_tdb, locking_key);
00442         }
00443 
00444         /* Copy any delete token plus the terminating filenames. */
00445         remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
00446         remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
00447 
00448         memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
00449                 remaining_ptr,
00450                 remaining_size);
00451 
00452         free(db_data.dptr);
00453 
00454         db_data.dptr = new_data_p;
00455 
00456         /* Re-save smaller record. */
00457         ld = (struct locking_data *)db_data.dptr;
00458         ld->u.s.num_share_mode_entries = num_share_modes;
00459 
00460         db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
00461 
00462         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
00463                 free(db_data.dptr);
00464                 return -1;
00465         }
00466         free(db_data.dptr);
00467         return 0;
00468 }
00469 
00470 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
00471                                 uint64_t dev,
00472                                 uint64_t ino,
00473                                 const struct smb_share_mode_entry *set_entry,
00474                                 const struct smb_share_mode_entry *new_entry)
00475 {
00476         TDB_DATA db_data;
00477         TDB_DATA locking_key =  get_locking_key(dev, ino);
00478         int num_share_modes = 0;
00479         struct locking_data *ld = NULL; /* internal samba db state. */
00480         struct share_mode_entry *shares = NULL;
00481         size_t i;
00482         int found_entry = 0;
00483 
00484         db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
00485         if (!db_data.dptr) {
00486                 return -1; /* Error - missing entry ! */
00487         }
00488 
00489         ld = (struct locking_data *)db_data.dptr;
00490         num_share_modes = ld->u.s.num_share_mode_entries;
00491         shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
00492 
00493         for (i = 0; i < num_share_modes; i++) {
00494                 struct share_mode_entry *share = &shares[i];
00495                 struct process_id pid = share->pid;
00496 
00497                 /* Check this process really exists. */
00498                 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
00499                         continue; /* No longer exists. */
00500                 }
00501 
00502                 if (share_mode_entry_equal(set_entry, share)) {
00503                         create_share_mode_entry(share, new_entry);
00504                         found_entry = 1;
00505                         break;
00506                 }
00507         }
00508 
00509         if (!found_entry) {
00510                 free(db_data.dptr);
00511                 return -1;
00512         }
00513 
00514         /* Save modified data. */
00515         if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
00516                 free(db_data.dptr);
00517                 return -1;
00518         }
00519         free(db_data.dptr);
00520         return 0;
00521 }

Sambaに対してSat Aug 29 21:23:06 2009に生成されました。  doxygen 1.4.7