Esempio n. 1
0
static krb5_error_code
NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
{
    krb5_error_code ret;
    struct ndbm_db *d = malloc(sizeof(*d));
    char *lock_file;

    if(d == NULL) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }
    asprintf(&lock_file, "%s.lock", (char*)db->hdb_name);
    if(lock_file == NULL) {
	free(d);
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }
    d->db = dbm_open((char*)db->hdb_name, flags, mode);
    if(d->db == NULL){
	ret = errno;
	free(d);
	free(lock_file);
	krb5_set_error_message(context, ret, "dbm_open(%s): %s", db->hdb_name,
			       strerror(ret));
	return ret;
    }
    d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
    if(d->lock_fd < 0){
	ret = errno;
	dbm_close(d->db);
	free(d);
	krb5_set_error_message(context, ret, "open(%s): %s", lock_file,
			       strerror(ret));
	free(lock_file);
	return ret;
    }
    free(lock_file);
    db->hdb_db = d;
    if((flags & O_ACCMODE) == O_RDONLY)
	ret = hdb_check_db_format(context, db);
    else
	ret = hdb_init_db(context, db);
    if(ret == HDB_ERR_NOENTRY)
	return 0;
    if (ret) {
	NDBM_close(context, db);
	krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
			       (flags & O_ACCMODE) == O_RDONLY ?
			       "checking format of" : "initialize",
			       db->hdb_name);
    }
    return ret;
}
Esempio n. 2
0
static krb5_error_code
mdb_open(krb5_context context, HDB *db, int flags, mode_t mode)
{
    char *fn;
    krb5_error_code ret;

    asprintf(&fn, "%s.db", db->hdb_name);
    if (fn == NULL) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }
    db->hdb_db = dbopen(fn, flags, mode, DB_BTREE, NULL);
    free(fn);

    if (db->hdb_db == NULL) {
	switch (errno) {
#ifdef EFTYPE
	case EFTYPE:
#endif
	case EINVAL:
	    db->hdb_db = dbopen(fn, flags, mode, DB_BTREE, NULL);
	}
    }

    /* try to open without .db extension */
    if(db->hdb_db == NULL && errno == ENOENT)
	db->hdb_db = dbopen(db->hdb_name, flags, mode, DB_BTREE, NULL);
    if(db->hdb_db == NULL) {
	ret = errno;
	krb5_set_error_message(context, ret, "dbopen (%s): %s",
			      db->hdb_name, strerror(ret));
	return ret;
    }
    if((flags & O_ACCMODE) == O_RDONLY)
	ret = hdb_check_db_format(context, db);
    else
	ret = hdb_init_db(context, db);
    if(ret == HDB_ERR_NOENTRY) {
	krb5_clear_error_message(context);
	return 0;
    }
    if (ret) {
	mdb_close(context, db);
	krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
			      (flags & O_ACCMODE) == O_RDONLY ?
			      "checking format of" : "initialize",
			      db->hdb_name);
    }
    return ret;
}
Esempio n. 3
0
static krb5_error_code
DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
{
    DB1_HDB *db1 = (DB1_HDB *)db;
    char *fn;
    krb5_error_code ret;

    asprintf(&fn, "%s.db", db->hdb_name);
    if (fn == NULL) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }
    db->hdb_db = _open_db(fn, flags, mode, &db1->lock_fd);
    free(fn);
    /* try to open without .db extension */
    if(db->hdb_db == NULL && errno == ENOENT)
	db->hdb_db = _open_db(db->hdb_name, flags, mode, &db1->lock_fd);
    if(db->hdb_db == NULL) {
	krb5_set_error_message(context, errno, "dbopen (%s): %s",
			      db->hdb_name, strerror(errno));
	return errno;
    }
    if((flags & O_ACCMODE) == O_RDONLY)
	ret = hdb_check_db_format(context, db);
    else
	ret = hdb_init_db(context, db);
    if(ret == HDB_ERR_NOENTRY) {
	krb5_clear_error_message(context);
	return 0;
    }
    if (ret) {
	DB_close(context, db);
	krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
			      (flags & O_ACCMODE) == O_RDONLY ?
			      "checking format of" : "initialize",
			      db->hdb_name);
    }
    return ret;
}
Esempio n. 4
0
static krb5_error_code
DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
{
    DBC *dbc = NULL;
    char *fn;
    krb5_error_code ret;
    DB *d;
    int myflags = 0;

    if (flags & O_CREAT)
      myflags |= DB_CREATE;

    if (flags & O_EXCL)
      myflags |= DB_EXCL;

    if((flags & O_ACCMODE) == O_RDONLY)
      myflags |= DB_RDONLY;

    if (flags & O_TRUNC)
      myflags |= DB_TRUNCATE;

    asprintf(&fn, "%s.db", db->hdb_name);
    if (fn == NULL) {
	krb5_set_error_string(context, "malloc: out of memory");
	return ENOMEM;
    }
    db_create(&d, NULL, 0);
    db->hdb_db = d;

#if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
    ret = (*d->open)(db->hdb_db, NULL, fn, NULL, DB_BTREE, myflags, mode);
#else
    ret = (*d->open)(db->hdb_db, fn, NULL, DB_BTREE, myflags, mode);
#endif

    if (ret == ENOENT) {
	/* try to open without .db extension */
#if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
	ret = (*d->open)(db->hdb_db, NULL, db->hdb_name, NULL, DB_BTREE,
			 myflags, mode);
#else
	ret = (*d->open)(db->hdb_db, db->hdb_name, NULL, DB_BTREE, 
			 myflags, mode);
#endif
    }

    if (ret) {
	free(fn);
	krb5_set_error_string(context, "opening %s: %s",
			      db->hdb_name, strerror(ret));
	return ret;
    }
    free(fn);

    ret = (*d->cursor)(d, NULL, &dbc, 0);
    if (ret) {
	krb5_set_error_string(context, "d->cursor: %s", strerror(ret));
        return ret;
    }
    db->hdb_dbc = dbc;

    if((flags & O_ACCMODE) == O_RDONLY)
	ret = hdb_check_db_format(context, db);
    else
	ret = hdb_init_db(context, db);
    if(ret == HDB_ERR_NOENTRY)
	return 0;
    if (ret) {
	DB_close(context, db);
	krb5_set_error_string(context, "hdb_open: failed %s database %s",
			      (flags & O_ACCMODE) == O_RDONLY ? 
			      "checking format of" : "initialize", 
			      db->hdb_name);
    }

    return ret;
}