/*
 *	Copyright 1988 by Rayan S. Zachariassen, all rights reserved.
 *	This will be free software, but only when it is finished.
 */

/* LINTLIBRARY */

#include "mailer.h"
#ifdef	HAVE_NDBM_H
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include <ndbm.h>
#include <sys/file.h>
#include "search.h"
#include "io.h"
#include "libz.h"
#include "libc.h"
#include "libsh.h"

/*
 * Search an NDBM format database for a key pair.
 */

conscell *
search_ndbm(sip)
	search_info *sip;
{
	DBM *db;
	datum val, key;
	conscell *tmp;
	struct spblk *spl;
	int retry;
	spkey_t symid;
	char *us;

	if (sip->file == NULL)
		return NULL;
	retry = 0;
	symid = symbol(sip->file);
	spl = sp_lookup(symid, spt_files);
	if (spl == NULL || (db = (DBM *)spl->data) == NULL) {
reopen:
		db = dbm_open((char* /* Sigh.. */)sip->file, O_RDONLY, 0); 
		if (db == NULL) {
			++deferit;
			v_set(DEFER, DEFER_IO_ERROR);
			fprintf(stderr, "search_ndbm: cannot open %s!\n",
					sip->file);
			return NULL;
		}
		if (spl == NULL)
			sp_install(symid, (void *)db, O_RDONLY, spt_files);
		else
			spl->data = (void *)db;
	}
	key.dptr  = (char*) sip->key; /* Sigh.. the cast.. */
	key.dsize = strlen(sip->key) + 1;
	val = dbm_fetch(db, key);
	if (val.dptr == NULL) {
#ifdef HAVE_DBM_ERROR
	  if (!retry && dbm_error(db)) {
	    dbm_close(db);
	    ++retry;
	    goto reopen;
	  }
#else
	  if (!retry && errno != 0) {
	    dbm_close(db);
	    ++retry;
	    goto reopen;
	  }
#endif
	  return NULL;
	}
	us = strnsave(val.dptr, val.dsize);
	return newstring(us);
}

/*
 * Flush buffered information from this database, close any file descriptors.
 */

void
close_ndbm(sip)
	search_info *sip;
{
	DBM *db;
	struct spblk *spl;
	spkey_t symid;

	if (sip->file == NULL)
		return;
	symid = symbol(sip->file);
	spl = sp_lookup(symid, spt_modcheck);
	if (spl != NULL)
		sp_delete(spl, spt_modcheck);
	spl = sp_lookup(symid, spt_files);
	if (spl == NULL || (db = (DBM *)spl->data) == NULL)
		return;
	dbm_close(db);
	sp_install(symid, (u_char *)NULL, 0, spt_files);
}


static DBM * open_ndbm __((search_info *, int, const char *));
static DBM *
open_ndbm(sip, flag, comment)
	search_info *sip;
	int flag;
	const char *comment;
{
	DBM *db;
	struct spblk *spl;
	spkey_t symid;

	if (sip->file == NULL)
		return NULL;
	symid = symbol(sip->file);
	spl = sp_lookup(symid, spt_files);
	if (spl != NULL && flag == O_RDWR && spl->mark != O_RDWR)
		close_ndbm(sip);
	if (spl == NULL || (db = (DBM *)spl->data) == NULL) {
		db = dbm_open((char * /* Sigh.. the cast.. */)sip->file,
			      flag, 0);
		if (db == NULL) {
			++deferit;
			v_set(DEFER, DEFER_IO_ERROR);
			fprintf(stderr, "%s: cannot open %s!\n",
					comment, sip->file);
			return NULL;
		}
		if (spl == NULL)
			sp_install(symid, (void *)db, flag, spt_files);
		else
			spl->data = (void *)db;
	}
	return db;
}

/*
 * Add the indicated key/value pair to the database.
 */

int
add_ndbm(sip, value)
	search_info *sip;
	const char *value;
{
	DBM *db;
	datum val, key;

	db = open_ndbm(sip, O_RDWR, "add_ndbm");
	if (db == NULL)
		return EOF;
	key.dptr  = (char*) sip->key;	/* Sigh.. the cast.. */
	key.dsize = strlen(sip->key) + 1;
	val.dptr  = (char*) value;	/* Sigh.. the cast.. */
	val.dsize = strlen(value) + 1;
	if (dbm_store(db, key, val, DBM_REPLACE) < 0) {
		++deferit;
		v_set(DEFER, DEFER_IO_ERROR);
		fprintf(stderr, "add_ndbm: cannot store (\"%s\",\"%s\")\n",
				sip->key, value);
		return EOF;
	}
	return 0;
}

/*
 * Remove the indicated key from the database.
 */

int
remove_ndbm(sip)
	search_info *sip;
{
	DBM *db;
	datum key;

	db = open_ndbm(sip, O_RDWR, "remove_ndbm");
	if (db == NULL)
		return EOF;
	key.dptr  = (char*) sip->key;	/* Sigh.. the cast.. */
	key.dsize = strlen(sip->key) + 1;
	if (dbm_delete(db, key) < 0) {
		++deferit;
		v_set(DEFER, DEFER_IO_ERROR);
		fprintf(stderr, "remove_ndbm: cannot remove \"%s\"\n",
				sip->key);
		return EOF;
	}
	return 0;
}

/*
 * Print the database.
 */

void
print_ndbm(sip, outfp)
	search_info *sip;
	FILE *outfp;
{
	DBM *db;
	datum key, val;

	db = open_ndbm(sip, O_RDONLY, "print_ndbm");
	if (db == NULL)
		return;
	for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db)) {
		val = dbm_fetch(db, key);
		if (val.dptr == NULL)
			continue;
#ifdef HAVE_DBM_ERROR
		if (dbm_error(db))
			break;
#else
		if (errno != 0)
			break;
#endif
		if (*(char*)val.dptr == '\0')
			fprintf(outfp, "%s\n", key.dptr);
		else
			fprintf(outfp, "%s\t%s\n", key.dptr, val.dptr);
	}
	fflush(outfp);
}

/*
 * Count the database.
 */

void
count_ndbm(sip, outfp)
	search_info *sip;
	FILE *outfp;
{
	DBM *db;
	datum key, val;
	int cnt = 0;

	db = open_ndbm(sip, O_RDONLY, "count_ndbm");
	if (db != NULL)
	  for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db)) {
	    val = dbm_fetch(db, key);
	    if (val.dptr == NULL)
	      continue;
#ifdef HAVE_DBM_ERROR
	    if (dbm_error(db))
	      break;
#else
	    if (errno != 0)
	      break;
#endif
	    ++cnt;
	}
	fprintf(outfp,"%d\n",cnt);
	fflush(outfp);
}

/*
 * Print the uid of the owner of the database.  Note that for ndbm-style
 * databases there are several files involved so picking one of them for
 * security purposes is very dubious.
 */

void
owner_ndbm(sip, outfp)
	search_info *sip;
	FILE *outfp;
{
	DBM *db;
	struct stat stbuf;

	db = open_ndbm(sip, O_RDONLY, "owner_ndbm");
	if (db == NULL)
		return;
	/* There are more timing hazards, when the internal fd is not
	   available for probing.. */
	if (fstat(dbm_dirfno(db), &stbuf) < 0) {
		fprintf(stderr, "owner_ndbm: cannot fstat(\"%s\")!\n",
				sip->file);
		return;
	}
	fprintf(outfp, "%d\n", stbuf.st_uid);
	fflush(outfp);
}

int
modp_ndbm(sip)
	search_info *sip;
{
	DBM *db;
	struct stat stbuf;
	struct spblk *spl;
	spkey_t symid;
	int rval;

	if (sip->file == NULL
	    || (db = open_ndbm(sip, O_RDONLY, "owner_ndbm")) == NULL)
		return 0;
	if (fstat(dbm_dirfno(db), &stbuf) < 0) {
		fprintf(stderr, "modp_ndbm: cannot fstat(\"%s\")!\n",
			sip->file);
		return 0;
	}
	if (stbuf.st_nlink == 0)
		return 1;	/* Unlinked underneath of us! */

	symid = symbol(sip->file);
	spl = sp_lookup(symid, spt_modcheck);
	if (spl != NULL) {
		rval = ((long)stbuf.st_mtime != (long)spl->data ||
			(long)stbuf.st_nlink != (long)spl->mark);
	} else
		rval = 0;
	sp_install(symid, (u_char *)((long)stbuf.st_mtime),
		   stbuf.st_nlink, spt_modcheck);
	return rval;
}
#endif	/* HAVE_NDBM */
