• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/netatalk-2.2.5/libatalk/cnid/cdb/
1/*
2 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
4 *
5 * CNID database support.
6 *
7 * here's the deal:
8 *  1) afpd already caches did's.
9 *  2) the database stores cnid's as both did/name and dev/ino pairs.
10 *  3) RootInfo holds the value of the NextID.
11 *  4) the cnid database gets called in the following manner --
12 *     start a database:
13 *     cnid = cnid_open(root_dir);
14 *
15 *     allocate a new id:
16 *     newid = cnid_add(cnid, dev, ino, parent did,
17 *     name, id); id is a hint for a specific id. pass 0 if you don't
18 *     care. if the id is already assigned, you won't get what you
19 *     requested.
20 *
21 *     given an id, get a did/name and dev/ino pair.
22 *     name = cnid_get(cnid, &id); given an id, return the corresponding
23 *     info.
24 *     return code = cnid_delete(cnid, id); delete an entry.
25 *
26 * with AFP, CNIDs 0-2 have special meanings. here they are:
27 * 0 -- invalid cnid
28 * 1 -- parent of root directory (handled by afpd)
29 * 2 -- root directory (handled by afpd)
30 *
31 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
32 * CNID_START begins at 17.
33 */
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif /* HAVE_CONFIG_H */
38
39#ifdef CNID_BACKEND_CDB
40
41#include <atalk/cnid_private.h>
42#include "cnid_cdb_private.h"
43
44#ifndef MIN
45#define MIN(a, b)  ((a) < (b) ? (a) : (b))
46#endif /* ! MIN */
47
48#define DBHOME        ".AppleDB"
49#define DBCNID        "cnid2.db"
50#define DBDEVINO      "devino.db"
51#define DBDIDNAME     "didname.db"      /* did/full name mapping */
52#define DBLOCKFILE    "cnid.lock"
53
54#define DBHOMELEN    8
55#define DBLEN        10
56
57#define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
58
59#define MAXITER     0xFFFF      /* maximum number of simultaneously open CNID
60                                 * databases. */
61
62static char *old_dbfiles[] = {"cnid.db", NULL};
63
64/* -----------------------
65 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
66*/
67static int my_yield(void)
68{
69    struct timeval t;
70    int ret;
71
72    t.tv_sec = 0;
73    t.tv_usec = 1000;
74    ret = select(0, NULL, NULL, NULL, &t);
75    return 0;
76}
77
78/* --------------- */
79static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
80{
81int len;
82
83    memset(skey, 0, sizeof(DBT));
84    skey->data = (char *)pdata->data + CNID_DID_OFS;
85    len = strlen((char *)skey->data + CNID_DID_LEN);
86    skey->size = CNID_DID_LEN + len + 1;
87    return (0);
88}
89
90/* --------------- */
91static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
92{
93    memset(skey, 0, sizeof(DBT));
94    skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
95    skey->size = CNID_DEVINO_LEN;
96    return (0);
97}
98
99/* --------------- */
100static int  my_associate (DB *p, DB *s,
101                   int (*callback)(DB *, const DBT *,const DBT *, DBT *),
102                   u_int32_t flags)
103{
104#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
105    return p->associate(p, NULL, s, callback, flags);
106#else
107#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
108    return p->associate(p,       s, callback, flags);
109#else
110    return 0; /* FIXME */
111#endif
112#endif
113}
114
115/* --------------- */
116static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
117{
118#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
119    return p->open(p, NULL, f, d, t, flags, mode);
120#else
121    return p->open(p, f, d, t, flags, mode);
122#endif
123}
124
125/* --------------- */
126static struct _cnid_db *cnid_cdb_new(const char *volpath)
127{
128    struct _cnid_db *cdb;
129    int major, minor, patch;
130    char *version_str;
131
132    version_str = db_version(&major, &minor, &patch);
133
134    /* check library match, ignore if only patch level changed */
135    if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
136    {
137        LOG(log_error, logtype_cnid, "cnid_cdb_new: the Berkeley DB library version used does not match the version compiled with: (%u.%u)/(%u.%u)", DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
138	return NULL;
139    }
140
141    if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
142        return NULL;
143
144    if ((cdb->volpath = strdup(volpath)) == NULL) {
145        free(cdb);
146        return NULL;
147    }
148
149    cdb->flags = CNID_FLAG_PERSISTENT;
150
151    cdb->cnid_add = cnid_cdb_add;
152    cdb->cnid_delete = cnid_cdb_delete;
153    cdb->cnid_get = cnid_cdb_get;
154    cdb->cnid_lookup = cnid_cdb_lookup;
155    cdb->cnid_nextid = NULL;    /*cnid_cdb_nextid;*/
156    cdb->cnid_resolve = cnid_cdb_resolve;
157    cdb->cnid_update = cnid_cdb_update;
158    cdb->cnid_close = cnid_cdb_close;
159    cdb->cnid_getstamp = cnid_cdb_getstamp;
160    cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
161
162    return cdb;
163}
164
165/* --------------- */
166static int upgrade_required(char *dbdir)
167{
168    char path[MAXPATHLEN + 1];
169    int len, i;
170    int found = 0;
171    struct stat st;
172
173    strcpy(path, dbdir);
174
175    len = strlen(path);
176    if (path[len - 1] != '/') {
177        strcat(path, "/");
178        len++;
179    }
180
181    for (i = 0; old_dbfiles[i] != NULL; i++) {
182	strcpy(path + len, old_dbfiles[i]);
183	if ( !(stat(path, &st) < 0) ) {
184	    found++;
185	    continue;
186	}
187	if (errno != ENOENT) {
188	    LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
189	    found++;
190	}
191    }
192    return found;
193}
194
195/* --------------- */
196struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
197{
198    struct stat st;
199    char path[MAXPATHLEN + 1];
200    CNID_private *db;
201    struct _cnid_db *cdb;
202    int open_flag, len;
203    static int first = 0;
204    int rc;
205
206    if (!args->dir || *args->dir == 0) {
207        return NULL;
208    }
209
210    /* this checks .AppleDB.
211       We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
212    if ((len = strlen(args->dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
213        LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", args->dir);
214        return NULL;
215    }
216
217    if ((cdb = cnid_cdb_new(args->dir)) == NULL) {
218        LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
219        return NULL;
220    }
221
222    if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
223        LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
224        goto fail_cdb;
225    }
226
227    cdb->_private = (void *) db;
228    db->magic = CNID_DB_MAGIC;
229
230    strcpy(path, args->dir);
231    if (path[len - 1] != '/') {
232        strcat(path, "/");
233        len++;
234    }
235
236    strcpy(path + len, DBHOME);
237    if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~args->mask) < 0)) {
238        LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
239        goto fail_adouble;
240    }
241
242    if (upgrade_required(path)) {
243	LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
244	goto fail_adouble;
245    }
246
247    /* Print out the version of BDB we're linked against. */
248    if (!first) {
249        first = 1;
250        LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
251    }
252
253    open_flag = DB_CREATE;
254
255    /* We need to be able to open the database environment with full
256     * transaction, logging, and locking support if we ever hope to
257     * be a true multi-acess file server. */
258    if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
259        LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
260        goto fail_lock;
261    }
262
263    /* Open the database environment. */
264    if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~args->mask)) != 0) {
265	LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
266	/* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
267        if (rc == DB_RUNRECOVERY) {
268            /* This is the mother of all errors.  We _must_ fail here. */
269            LOG(log_error, logtype_default,
270                "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
271            goto fail_lock;
272        }
273
274        /* We can't get a full transactional environment, so multi-access
275         * is out of the question.  Let's assume a read-only environment,
276         * and try to at least get a shared memory pool. */
277        if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~args->mask)) != 0) {
278            /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
279             * open the environment with no flags. */
280            if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~args->mask)) != 0) {
281                LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
282                goto fail_lock;
283            }
284        }
285        db->flags |= CNIDFLAG_DB_RO;
286        open_flag = DB_RDONLY;
287        LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
288    }
289
290    /* ---------------------- */
291    /* Main CNID database.  Use a hash for this one. */
292
293    if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
294        LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
295            db_strerror(rc));
296        goto fail_appinit;
297    }
298
299    if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
300        LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
301            db_strerror(rc));
302        goto fail_appinit;
303    }
304
305    /* ---------------------- */
306    /* did/name reverse mapping.  We use a BTree for this one. */
307
308    if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
309        LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
310            db_strerror(rc));
311        goto fail_appinit;
312    }
313
314    if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~args->mask))) {
315        LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
316            db_strerror(rc));
317        goto fail_appinit;
318    }
319
320    /* ---------------------- */
321    /* dev/ino reverse mapping.  Use a hash for this one. */
322
323    if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
324        LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
325            db_strerror(rc));
326        goto fail_appinit;
327    }
328
329    if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
330        LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
331            db_strerror(rc));
332        goto fail_appinit;
333    }
334
335    /* ---------------------- */
336    /* Associate the secondary with the primary. */
337    if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
338        LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
339            db_strerror(rc));
340        goto fail_appinit;
341    }
342
343    if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
344        LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
345            db_strerror(rc));
346        goto fail_appinit;
347    }
348
349    /* ---------------------- */
350    /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
351
352    DBT key, data;
353    uint32_t version;
354
355    memset(&key, 0, sizeof(key));
356    memset(&data, 0, sizeof(data));
357    key.data = ROOTINFO_KEY;
358    key.size = ROOTINFO_KEYLEN;
359
360    if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0)) == 0) {
361        /* If not found, ignore it */
362        memcpy(&version, data.data + CNID_DID_OFS, sizeof(version));
363        version = ntohl(version);
364        LOG(log_debug, logtype_default, "CNID db version %u", version);
365        if (version != CNID_VERSION_0) {
366            LOG(log_error, logtype_default, "Unsupported CNID db version %u, use CNID backend \"dbd\"", version);
367            goto fail_appinit;
368        }
369    }
370
371    db_env_set_func_yield(my_yield);
372    return cdb;
373
374  fail_appinit:
375    if (db->db_didname)
376        db->db_didname->close(db->db_didname, 0);
377    if (db->db_devino)
378        db->db_devino->close(db->db_devino, 0);
379    if (db->db_cnid)
380        db->db_cnid->close(db->db_cnid, 0);
381    LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
382    db->dbenv->close(db->dbenv, 0);
383
384  fail_lock:
385
386  fail_adouble:
387
388    free(db);
389
390  fail_cdb:
391    if (cdb->volpath != NULL)
392        free(cdb->volpath);
393    free(cdb);
394
395    return NULL;
396}
397
398struct _cnid_module cnid_cdb_module = {
399    "cdb",
400    {NULL, NULL},
401    cnid_cdb_open,
402    CNID_FLAG_SETUID | CNID_FLAG_BLOCK
403};
404
405
406#endif /* CNID_BACKEND_CDB */
407