• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/netatalk-3.0.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/* --------------- */
65static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
66{
67int len;
68
69    memset(skey, 0, sizeof(DBT));
70    skey->data = (char *)pdata->data + CNID_DID_OFS;
71    len = strlen((char *)skey->data + CNID_DID_LEN);
72    skey->size = CNID_DID_LEN + len + 1;
73    return (0);
74}
75
76/* --------------- */
77static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
78{
79    memset(skey, 0, sizeof(DBT));
80    skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
81    skey->size = CNID_DEVINO_LEN;
82    return (0);
83}
84
85/* --------------- */
86static int  my_associate (DB *p, DB *s,
87                   int (*callback)(DB *, const DBT *,const DBT *, DBT *),
88                   u_int32_t flags)
89{
90#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
91    return p->associate(p, NULL, s, callback, flags);
92#else
93#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
94    return p->associate(p,       s, callback, flags);
95#else
96    return 0; /* FIXME */
97#endif
98#endif
99}
100
101/* --------------- */
102static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
103{
104#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
105    return p->open(p, NULL, f, d, t, flags, mode);
106#else
107    return p->open(p, f, d, t, flags, mode);
108#endif
109}
110
111/* --------------- */
112static struct _cnid_db *cnid_cdb_new(const char *volpath)
113{
114    struct _cnid_db *cdb;
115    int major, minor, patch;
116    char *version_str;
117
118    version_str = db_version(&major, &minor, &patch);
119
120    /* check library match, ignore if only patch level changed */
121    if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
122    {
123        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);
124	return NULL;
125    }
126
127    if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
128        return NULL;
129
130    if ((cdb->volpath = strdup(volpath)) == NULL) {
131        free(cdb);
132        return NULL;
133    }
134
135    cdb->flags = CNID_FLAG_PERSISTENT;
136
137    cdb->cnid_add = cnid_cdb_add;
138    cdb->cnid_delete = cnid_cdb_delete;
139    cdb->cnid_get = cnid_cdb_get;
140    cdb->cnid_lookup = cnid_cdb_lookup;
141    cdb->cnid_nextid = NULL;    /*cnid_cdb_nextid;*/
142    cdb->cnid_resolve = cnid_cdb_resolve;
143    cdb->cnid_update = cnid_cdb_update;
144    cdb->cnid_close = cnid_cdb_close;
145    cdb->cnid_getstamp = cnid_cdb_getstamp;
146    cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
147    cdb->cnid_wipe = NULL;
148    return cdb;
149}
150
151/* --------------- */
152static int upgrade_required(char *dbdir)
153{
154    char path[MAXPATHLEN + 1];
155    int len, i;
156    int found = 0;
157    struct stat st;
158
159    strcpy(path, dbdir);
160
161    len = strlen(path);
162    if (path[len - 1] != '/') {
163        strcat(path, "/");
164        len++;
165    }
166
167    for (i = 0; old_dbfiles[i] != NULL; i++) {
168	strcpy(path + len, old_dbfiles[i]);
169	if ( !(stat(path, &st) < 0) ) {
170	    found++;
171	    continue;
172	}
173	if (errno != ENOENT) {
174	    LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
175	    found++;
176	}
177    }
178    return found;
179}
180
181/* --------------- */
182struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
183{
184    struct stat st;
185    char path[MAXPATHLEN + 1];
186    CNID_private *db;
187    struct _cnid_db *cdb;
188    int open_flag, len;
189    static int first = 0;
190    int rc;
191
192    if (!args->dir || *args->dir == 0) {
193        return NULL;
194    }
195
196    /* this checks .AppleDB.
197       We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
198    if ((len = strlen(args->dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
199        LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", args->dir);
200        return NULL;
201    }
202
203    if ((cdb = cnid_cdb_new(args->dir)) == NULL) {
204        LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
205        return NULL;
206    }
207
208    if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
209        LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
210        goto fail_cdb;
211    }
212
213    cdb->_private = (void *) db;
214    db->magic = CNID_DB_MAGIC;
215
216    strcpy(path, args->dir);
217    if (path[len - 1] != '/') {
218        strcat(path, "/");
219        len++;
220    }
221
222    strcpy(path + len, DBHOME);
223    if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~args->mask) < 0)) {
224        LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
225        goto fail_adouble;
226    }
227
228    if (upgrade_required(path)) {
229	LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
230	goto fail_adouble;
231    }
232
233    /* Print out the version of BDB we're linked against. */
234    if (!first) {
235        first = 1;
236        LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
237    }
238
239    open_flag = DB_CREATE;
240
241    /* We need to be able to open the database environment with full
242     * transaction, logging, and locking support if we ever hope to
243     * be a true multi-acess file server. */
244    if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
245        LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
246        goto fail_lock;
247    }
248
249    /* Open the database environment. */
250    if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~args->mask)) != 0) {
251	LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
252	/* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
253        if (rc == DB_RUNRECOVERY) {
254            /* This is the mother of all errors.  We _must_ fail here. */
255            LOG(log_error, logtype_default,
256                "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
257            goto fail_lock;
258        }
259
260        /* We can't get a full transactional environment, so multi-access
261         * is out of the question.  Let's assume a read-only environment,
262         * and try to at least get a shared memory pool. */
263        if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~args->mask)) != 0) {
264            /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
265             * open the environment with no flags. */
266            if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~args->mask)) != 0) {
267                LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
268                goto fail_lock;
269            }
270        }
271        db->flags |= CNIDFLAG_DB_RO;
272        open_flag = DB_RDONLY;
273        LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
274    }
275
276    /* ---------------------- */
277    /* Main CNID database.  Use a hash for this one. */
278
279    if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
280        LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
281            db_strerror(rc));
282        goto fail_appinit;
283    }
284
285    if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
286        LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
287            db_strerror(rc));
288        goto fail_appinit;
289    }
290
291    /* ---------------------- */
292    /* did/name reverse mapping.  We use a BTree for this one. */
293
294    if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
295        LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
296            db_strerror(rc));
297        goto fail_appinit;
298    }
299
300    if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~args->mask))) {
301        LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
302            db_strerror(rc));
303        goto fail_appinit;
304    }
305
306    /* ---------------------- */
307    /* dev/ino reverse mapping.  Use a hash for this one. */
308
309    if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
310        LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
311            db_strerror(rc));
312        goto fail_appinit;
313    }
314
315    if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
316        LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
317            db_strerror(rc));
318        goto fail_appinit;
319    }
320
321    /* ---------------------- */
322    /* Associate the secondary with the primary. */
323    if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
324        LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
325            db_strerror(rc));
326        goto fail_appinit;
327    }
328
329    if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
330        LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
331            db_strerror(rc));
332        goto fail_appinit;
333    }
334
335    /* ---------------------- */
336    /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
337
338    DBT key, data;
339    uint32_t version;
340
341    memset(&key, 0, sizeof(key));
342    memset(&data, 0, sizeof(data));
343    key.data = ROOTINFO_KEY;
344    key.size = ROOTINFO_KEYLEN;
345
346    if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0)) == 0) {
347        /* If not found, ignore it */
348        memcpy(&version, data.data + CNID_DID_OFS, sizeof(version));
349        version = ntohl(version);
350        LOG(log_debug, logtype_default, "CNID db version %u", version);
351        if (version != CNID_VERSION_0) {
352            LOG(log_error, logtype_default, "Unsupported CNID db version %u, use CNID backend \"dbd\"", version);
353            goto fail_appinit;
354        }
355    }
356
357    return cdb;
358
359  fail_appinit:
360    if (db->db_didname)
361        db->db_didname->close(db->db_didname, 0);
362    if (db->db_devino)
363        db->db_devino->close(db->db_devino, 0);
364    if (db->db_cnid)
365        db->db_cnid->close(db->db_cnid, 0);
366    LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
367    db->dbenv->close(db->dbenv, 0);
368
369  fail_lock:
370
371  fail_adouble:
372
373    free(db);
374
375  fail_cdb:
376    if (cdb->volpath != NULL)
377        free(cdb->volpath);
378    free(cdb);
379
380    return NULL;
381}
382
383struct _cnid_module cnid_cdb_module = {
384    "cdb",
385    {NULL, NULL},
386    cnid_cdb_open,
387    CNID_FLAG_SETUID | CNID_FLAG_BLOCK
388};
389
390
391#endif /* CNID_BACKEND_CDB */
392