xref: /onnv-gate/usr/src/cmd/sendmail/libsmdb/smdb.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate ** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate **	All rights reserved.
4*0Sstevel@tonic-gate **
5*0Sstevel@tonic-gate ** By using this file, you agree to the terms and conditions set
6*0Sstevel@tonic-gate ** forth in the LICENSE file which can be found at the top level of
7*0Sstevel@tonic-gate ** the sendmail distribution.
8*0Sstevel@tonic-gate */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include <sm/gen.h>
13*0Sstevel@tonic-gate SM_RCSID("@(#)$Id: smdb.c,v 8.58 2004/08/03 20:58:38 ca Exp $")
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #include <fcntl.h>
16*0Sstevel@tonic-gate #include <stdlib.h>
17*0Sstevel@tonic-gate #include <unistd.h>
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include <sendmail/sendmail.h>
21*0Sstevel@tonic-gate #include <libsmdb/smdb.h>
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate static bool	smdb_lockfile __P((int, int));
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate ** SMDB_MALLOC_DATABASE -- Allocates a database structure.
27*0Sstevel@tonic-gate **
28*0Sstevel@tonic-gate **	Parameters:
29*0Sstevel@tonic-gate **		None
30*0Sstevel@tonic-gate **
31*0Sstevel@tonic-gate **	Returns:
32*0Sstevel@tonic-gate **		An pointer to an allocated SMDB_DATABASE structure or
33*0Sstevel@tonic-gate **		NULL if it couldn't allocate the memory.
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate SMDB_DATABASE *
smdb_malloc_database()37*0Sstevel@tonic-gate smdb_malloc_database()
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	SMDB_DATABASE *db;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	db = (SMDB_DATABASE *) malloc(sizeof(SMDB_DATABASE));
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	if (db != NULL)
44*0Sstevel@tonic-gate 		(void) memset(db, '\0', sizeof(SMDB_DATABASE));
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	return db;
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate ** SMDB_FREE_DATABASE -- Unallocates a database structure.
52*0Sstevel@tonic-gate **
53*0Sstevel@tonic-gate **	Parameters:
54*0Sstevel@tonic-gate **		database -- a SMDB_DATABASE pointer to deallocate.
55*0Sstevel@tonic-gate **
56*0Sstevel@tonic-gate **	Returns:
57*0Sstevel@tonic-gate **		None
58*0Sstevel@tonic-gate */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate void
smdb_free_database(database)61*0Sstevel@tonic-gate smdb_free_database(database)
62*0Sstevel@tonic-gate 	SMDB_DATABASE *database;
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	if (database != NULL)
65*0Sstevel@tonic-gate 		free(database);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate **  SMDB_LOCKFILE -- lock a file using flock or (shudder) fcntl locking
69*0Sstevel@tonic-gate **
70*0Sstevel@tonic-gate **	Parameters:
71*0Sstevel@tonic-gate **		fd -- the file descriptor of the file.
72*0Sstevel@tonic-gate **		type -- type of the lock.  Bits can be:
73*0Sstevel@tonic-gate **			LOCK_EX -- exclusive lock.
74*0Sstevel@tonic-gate **			LOCK_NB -- non-blocking.
75*0Sstevel@tonic-gate **
76*0Sstevel@tonic-gate **	Returns:
77*0Sstevel@tonic-gate **		true if the lock was acquired.
78*0Sstevel@tonic-gate **		false otherwise.
79*0Sstevel@tonic-gate */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static bool
smdb_lockfile(fd,type)82*0Sstevel@tonic-gate smdb_lockfile(fd, type)
83*0Sstevel@tonic-gate 	int fd;
84*0Sstevel@tonic-gate 	int type;
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	int i;
87*0Sstevel@tonic-gate 	int save_errno;
88*0Sstevel@tonic-gate #if !HASFLOCK
89*0Sstevel@tonic-gate 	int action;
90*0Sstevel@tonic-gate 	struct flock lfd;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	(void) memset(&lfd, '\0', sizeof lfd);
93*0Sstevel@tonic-gate 	if (bitset(LOCK_UN, type))
94*0Sstevel@tonic-gate 		lfd.l_type = F_UNLCK;
95*0Sstevel@tonic-gate 	else if (bitset(LOCK_EX, type))
96*0Sstevel@tonic-gate 		lfd.l_type = F_WRLCK;
97*0Sstevel@tonic-gate 	else
98*0Sstevel@tonic-gate 		lfd.l_type = F_RDLCK;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if (bitset(LOCK_NB, type))
101*0Sstevel@tonic-gate 		action = F_SETLK;
102*0Sstevel@tonic-gate 	else
103*0Sstevel@tonic-gate 		action = F_SETLKW;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
106*0Sstevel@tonic-gate 		continue;
107*0Sstevel@tonic-gate 	if (i >= 0)
108*0Sstevel@tonic-gate 		return true;
109*0Sstevel@tonic-gate 	save_errno = errno;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/*
112*0Sstevel@tonic-gate 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
113*0Sstevel@tonic-gate 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
114*0Sstevel@tonic-gate 	**  as type "tmp" (that is, served from swap space), the
115*0Sstevel@tonic-gate 	**  previous fcntl will fail with "Invalid argument" errors.
116*0Sstevel@tonic-gate 	**  Since this is fairly common during testing, we will assume
117*0Sstevel@tonic-gate 	**  that this indicates that the lock is successfully grabbed.
118*0Sstevel@tonic-gate 	*/
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (save_errno == EINVAL)
121*0Sstevel@tonic-gate 		return true;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (!bitset(LOCK_NB, type) ||
124*0Sstevel@tonic-gate 	    (save_errno != EACCES && save_errno != EAGAIN))
125*0Sstevel@tonic-gate 	{
126*0Sstevel@tonic-gate # if 0
127*0Sstevel@tonic-gate 		int omode = fcntl(fd, F_GETFL, NULL);
128*0Sstevel@tonic-gate 		int euid = (int) geteuid();
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		syslog(LOG_ERR, "cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
131*0Sstevel@tonic-gate 		       filename, ext, fd, type, omode, euid);
132*0Sstevel@tonic-gate # endif /* 0 */
133*0Sstevel@tonic-gate 		errno = save_errno;
134*0Sstevel@tonic-gate 		return false;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate #else /* !HASFLOCK */
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	while ((i = flock(fd, type)) < 0 && errno == EINTR)
139*0Sstevel@tonic-gate 		continue;
140*0Sstevel@tonic-gate 	if (i >= 0)
141*0Sstevel@tonic-gate 		return true;
142*0Sstevel@tonic-gate 	save_errno = errno;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
145*0Sstevel@tonic-gate 	{
146*0Sstevel@tonic-gate # if 0
147*0Sstevel@tonic-gate 		int omode = fcntl(fd, F_GETFL, NULL);
148*0Sstevel@tonic-gate 		int euid = (int) geteuid();
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		syslog(LOG_ERR, "cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
151*0Sstevel@tonic-gate 		       filename, ext, fd, type, omode, euid);
152*0Sstevel@tonic-gate # endif /* 0 */
153*0Sstevel@tonic-gate 		errno = save_errno;
154*0Sstevel@tonic-gate 		return false;
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate #endif /* !HASFLOCK */
157*0Sstevel@tonic-gate 	errno = save_errno;
158*0Sstevel@tonic-gate 	return false;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate ** SMDB_OPEN_DATABASE -- Opens a database.
162*0Sstevel@tonic-gate **
163*0Sstevel@tonic-gate **	This opens a database. If type is SMDB_DEFAULT it tries to
164*0Sstevel@tonic-gate **	use a DB1 or DB2 hash. If that isn't available, it will try
165*0Sstevel@tonic-gate **	to use NDBM. If a specific type is given it will try to open
166*0Sstevel@tonic-gate **	a database of that type.
167*0Sstevel@tonic-gate **
168*0Sstevel@tonic-gate **	Parameters:
169*0Sstevel@tonic-gate **		database -- An pointer to a SMDB_DATABASE pointer where the
170*0Sstevel@tonic-gate **			   opened database will be stored. This should
171*0Sstevel@tonic-gate **			   be unallocated.
172*0Sstevel@tonic-gate **		db_name -- The name of the database to open. Do not include
173*0Sstevel@tonic-gate **			  the file name extension.
174*0Sstevel@tonic-gate **		mode -- The mode to set on the database file or files.
175*0Sstevel@tonic-gate **		mode_mask -- Mode bits that must match on an opened database.
176*0Sstevel@tonic-gate **		sff -- Flags to safefile.
177*0Sstevel@tonic-gate **		type -- The type of database to open. Supported types
178*0Sstevel@tonic-gate **		       vary depending on what was compiled in.
179*0Sstevel@tonic-gate **		user_info -- Information on the user to use for file
180*0Sstevel@tonic-gate **			    permissions.
181*0Sstevel@tonic-gate **		params -- Params specific to the database being opened.
182*0Sstevel@tonic-gate **			 Only supports some DB hash options right now
183*0Sstevel@tonic-gate **			 (see smdb_db_open() for details).
184*0Sstevel@tonic-gate **
185*0Sstevel@tonic-gate **	Returns:
186*0Sstevel@tonic-gate **		SMDBE_OK -- Success.
187*0Sstevel@tonic-gate **		Anything else is an error. Look up more info about the
188*0Sstevel@tonic-gate **		error in the comments for the specific open() used.
189*0Sstevel@tonic-gate */
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate int
smdb_open_database(database,db_name,mode,mode_mask,sff,type,user_info,params)192*0Sstevel@tonic-gate smdb_open_database(database, db_name, mode, mode_mask, sff, type, user_info,
193*0Sstevel@tonic-gate 		   params)
194*0Sstevel@tonic-gate 	SMDB_DATABASE **database;
195*0Sstevel@tonic-gate 	char *db_name;
196*0Sstevel@tonic-gate 	int mode;
197*0Sstevel@tonic-gate 	int mode_mask;
198*0Sstevel@tonic-gate 	long sff;
199*0Sstevel@tonic-gate 	SMDB_DBTYPE type;
200*0Sstevel@tonic-gate 	SMDB_USER_INFO *user_info;
201*0Sstevel@tonic-gate 	SMDB_DBPARAMS *params;
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	bool type_was_default = false;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (type == SMDB_TYPE_DEFAULT)
206*0Sstevel@tonic-gate 	{
207*0Sstevel@tonic-gate 		type_was_default = true;
208*0Sstevel@tonic-gate #ifdef NEWDB
209*0Sstevel@tonic-gate 		type = SMDB_TYPE_HASH;
210*0Sstevel@tonic-gate #else /* NEWDB */
211*0Sstevel@tonic-gate # ifdef NDBM
212*0Sstevel@tonic-gate 		type = SMDB_TYPE_NDBM;
213*0Sstevel@tonic-gate # endif /* NDBM */
214*0Sstevel@tonic-gate #endif /* NEWDB */
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (type == SMDB_TYPE_DEFAULT)
218*0Sstevel@tonic-gate 		return SMDBE_UNKNOWN_DB_TYPE;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if ((strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) ||
221*0Sstevel@tonic-gate 	    (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0))
222*0Sstevel@tonic-gate 	{
223*0Sstevel@tonic-gate #ifdef NEWDB
224*0Sstevel@tonic-gate 		int result;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		result = smdb_db_open(database, db_name, mode, mode_mask, sff,
227*0Sstevel@tonic-gate 				      type, user_info, params);
228*0Sstevel@tonic-gate # ifdef NDBM
229*0Sstevel@tonic-gate 		if (result == ENOENT && type_was_default)
230*0Sstevel@tonic-gate 			type = SMDB_TYPE_NDBM;
231*0Sstevel@tonic-gate 		else
232*0Sstevel@tonic-gate # endif /* NDBM */
233*0Sstevel@tonic-gate 			return result;
234*0Sstevel@tonic-gate #else /* NEWDB */
235*0Sstevel@tonic-gate 		return SMDBE_UNSUPPORTED_DB_TYPE;
236*0Sstevel@tonic-gate #endif /* NEWDB */
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	if (strncmp(type, SMDB_TYPE_NDBM, SMDB_TYPE_NDBM_LEN) == 0)
240*0Sstevel@tonic-gate 	{
241*0Sstevel@tonic-gate #ifdef NDBM
242*0Sstevel@tonic-gate 		int result;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 		result = smdb_ndbm_open(database, db_name, mode, mode_mask,
245*0Sstevel@tonic-gate 					sff, type, user_info, params);
246*0Sstevel@tonic-gate 		return result;
247*0Sstevel@tonic-gate #else /* NDBM */
248*0Sstevel@tonic-gate 		return SMDBE_UNSUPPORTED_DB_TYPE;
249*0Sstevel@tonic-gate #endif /* NDBM */
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	return SMDBE_UNKNOWN_DB_TYPE;
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate /*
255*0Sstevel@tonic-gate ** SMDB_ADD_EXTENSION -- Adds an extension to a file name.
256*0Sstevel@tonic-gate **
257*0Sstevel@tonic-gate **	Just adds a . followed by a string to a db_name if there
258*0Sstevel@tonic-gate **	is room and the db_name does not already have that extension.
259*0Sstevel@tonic-gate **
260*0Sstevel@tonic-gate **	Parameters:
261*0Sstevel@tonic-gate **		full_name -- The final file name.
262*0Sstevel@tonic-gate **		max_full_name_len -- The max length for full_name.
263*0Sstevel@tonic-gate **		db_name -- The name of the db.
264*0Sstevel@tonic-gate **		extension -- The extension to add.
265*0Sstevel@tonic-gate **
266*0Sstevel@tonic-gate **	Returns:
267*0Sstevel@tonic-gate **		SMDBE_OK -- Success.
268*0Sstevel@tonic-gate **		Anything else is an error. Look up more info about the
269*0Sstevel@tonic-gate **		error in the comments for the specific open() used.
270*0Sstevel@tonic-gate */
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate int
smdb_add_extension(full_name,max_full_name_len,db_name,extension)273*0Sstevel@tonic-gate smdb_add_extension(full_name, max_full_name_len, db_name, extension)
274*0Sstevel@tonic-gate 	char *full_name;
275*0Sstevel@tonic-gate 	int max_full_name_len;
276*0Sstevel@tonic-gate 	char *db_name;
277*0Sstevel@tonic-gate 	char *extension;
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate 	int extension_len;
280*0Sstevel@tonic-gate 	int db_name_len;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (full_name == NULL || db_name == NULL || extension == NULL)
283*0Sstevel@tonic-gate 		return SMDBE_INVALID_PARAMETER;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	extension_len = strlen(extension);
286*0Sstevel@tonic-gate 	db_name_len = strlen(db_name);
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	if (extension_len + db_name_len + 2 > max_full_name_len)
289*0Sstevel@tonic-gate 		return SMDBE_DB_NAME_TOO_LONG;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	if (db_name_len < extension_len + 1 ||
292*0Sstevel@tonic-gate 	    db_name[db_name_len - extension_len - 1] != '.' ||
293*0Sstevel@tonic-gate 	    strcmp(&db_name[db_name_len - extension_len], extension) != 0)
294*0Sstevel@tonic-gate 		(void) sm_snprintf(full_name, max_full_name_len, "%s.%s",
295*0Sstevel@tonic-gate 				   db_name, extension);
296*0Sstevel@tonic-gate 	else
297*0Sstevel@tonic-gate 		(void) sm_strlcpy(full_name, db_name, max_full_name_len);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	return SMDBE_OK;
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate **  SMDB_LOCK_FILE -- Locks the database file.
303*0Sstevel@tonic-gate **
304*0Sstevel@tonic-gate **	Locks the actual database file.
305*0Sstevel@tonic-gate **
306*0Sstevel@tonic-gate **	Parameters:
307*0Sstevel@tonic-gate **		lock_fd -- The resulting descriptor for the locked file.
308*0Sstevel@tonic-gate **		db_name -- The name of the database without extension.
309*0Sstevel@tonic-gate **		mode -- The open mode.
310*0Sstevel@tonic-gate **		sff -- Flags to safefile.
311*0Sstevel@tonic-gate **		extension -- The extension for the file.
312*0Sstevel@tonic-gate **
313*0Sstevel@tonic-gate **	Returns:
314*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
315*0Sstevel@tonic-gate */
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate int
smdb_lock_file(lock_fd,db_name,mode,sff,extension)318*0Sstevel@tonic-gate smdb_lock_file(lock_fd, db_name, mode, sff, extension)
319*0Sstevel@tonic-gate 	int *lock_fd;
320*0Sstevel@tonic-gate 	char *db_name;
321*0Sstevel@tonic-gate 	int mode;
322*0Sstevel@tonic-gate 	long sff;
323*0Sstevel@tonic-gate 	char *extension;
324*0Sstevel@tonic-gate {
325*0Sstevel@tonic-gate 	int result;
326*0Sstevel@tonic-gate 	char file_name[MAXPATHLEN];
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	result = smdb_add_extension(file_name, sizeof file_name, db_name,
329*0Sstevel@tonic-gate 				    extension);
330*0Sstevel@tonic-gate 	if (result != SMDBE_OK)
331*0Sstevel@tonic-gate 		return result;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	*lock_fd = safeopen(file_name, mode & ~O_TRUNC, DBMMODE, sff);
334*0Sstevel@tonic-gate 	if (*lock_fd < 0)
335*0Sstevel@tonic-gate 		return errno;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	return SMDBE_OK;
338*0Sstevel@tonic-gate }
339*0Sstevel@tonic-gate /*
340*0Sstevel@tonic-gate **  SMDB_UNLOCK_FILE -- Unlocks a file
341*0Sstevel@tonic-gate **
342*0Sstevel@tonic-gate **	Unlocks a file.
343*0Sstevel@tonic-gate **
344*0Sstevel@tonic-gate **	Parameters:
345*0Sstevel@tonic-gate **		lock_fd -- The descriptor for the locked file.
346*0Sstevel@tonic-gate **
347*0Sstevel@tonic-gate **	Returns:
348*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
349*0Sstevel@tonic-gate */
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate int
smdb_unlock_file(lock_fd)352*0Sstevel@tonic-gate smdb_unlock_file(lock_fd)
353*0Sstevel@tonic-gate 	int lock_fd;
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	int result;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	result = close(lock_fd);
358*0Sstevel@tonic-gate 	if (result != 0)
359*0Sstevel@tonic-gate 		return errno;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	return SMDBE_OK;
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate **  SMDB_LOCK_MAP -- Locks a database.
365*0Sstevel@tonic-gate **
366*0Sstevel@tonic-gate **	Parameters:
367*0Sstevel@tonic-gate **		database -- database description.
368*0Sstevel@tonic-gate **		type -- type of the lock.  Bits can be:
369*0Sstevel@tonic-gate **			LOCK_EX -- exclusive lock.
370*0Sstevel@tonic-gate **			LOCK_NB -- non-blocking.
371*0Sstevel@tonic-gate **
372*0Sstevel@tonic-gate **	Returns:
373*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
374*0Sstevel@tonic-gate */
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate int
smdb_lock_map(database,type)377*0Sstevel@tonic-gate smdb_lock_map(database, type)
378*0Sstevel@tonic-gate 	SMDB_DATABASE *database;
379*0Sstevel@tonic-gate 	int type;
380*0Sstevel@tonic-gate {
381*0Sstevel@tonic-gate 	int fd;
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	fd = database->smdb_lockfd(database);
384*0Sstevel@tonic-gate 	if (fd < 0)
385*0Sstevel@tonic-gate 		return SMDBE_NOT_FOUND;
386*0Sstevel@tonic-gate 	if (!smdb_lockfile(fd, type))
387*0Sstevel@tonic-gate 		return SMDBE_LOCK_NOT_GRANTED;
388*0Sstevel@tonic-gate 	return SMDBE_OK;
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate /*
391*0Sstevel@tonic-gate **  SMDB_UNLOCK_MAP -- Unlocks a database
392*0Sstevel@tonic-gate **
393*0Sstevel@tonic-gate **	Parameters:
394*0Sstevel@tonic-gate **		database -- database description.
395*0Sstevel@tonic-gate **
396*0Sstevel@tonic-gate **	Returns:
397*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
398*0Sstevel@tonic-gate */
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate int
smdb_unlock_map(database)401*0Sstevel@tonic-gate smdb_unlock_map(database)
402*0Sstevel@tonic-gate 	SMDB_DATABASE *database;
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate 	int fd;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	fd = database->smdb_lockfd(database);
407*0Sstevel@tonic-gate 	if (fd < 0)
408*0Sstevel@tonic-gate 		return SMDBE_NOT_FOUND;
409*0Sstevel@tonic-gate 	if (!smdb_lockfile(fd, LOCK_UN))
410*0Sstevel@tonic-gate 		return SMDBE_LOCK_NOT_HELD;
411*0Sstevel@tonic-gate 	return SMDBE_OK;
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate /*
414*0Sstevel@tonic-gate **  SMDB_SETUP_FILE -- Gets db file ready for use.
415*0Sstevel@tonic-gate **
416*0Sstevel@tonic-gate **	Makes sure permissions on file are safe and creates it if it
417*0Sstevel@tonic-gate **	doesn't exist.
418*0Sstevel@tonic-gate **
419*0Sstevel@tonic-gate **	Parameters:
420*0Sstevel@tonic-gate **		db_name -- The name of the database without extension.
421*0Sstevel@tonic-gate **		extension -- The extension.
422*0Sstevel@tonic-gate **		sff -- Flags to safefile.
423*0Sstevel@tonic-gate **		mode_mask -- Mode bits that must match.
424*0Sstevel@tonic-gate **		user_info -- Information on the user to use for file
425*0Sstevel@tonic-gate **			    permissions.
426*0Sstevel@tonic-gate **		stat_info -- A place to put the stat info for the file.
427*0Sstevel@tonic-gate **	Returns:
428*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
429*0Sstevel@tonic-gate */
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate int
smdb_setup_file(db_name,extension,mode_mask,sff,user_info,stat_info)432*0Sstevel@tonic-gate smdb_setup_file(db_name, extension, mode_mask, sff, user_info, stat_info)
433*0Sstevel@tonic-gate 	char *db_name;
434*0Sstevel@tonic-gate 	char *extension;
435*0Sstevel@tonic-gate 	int mode_mask;
436*0Sstevel@tonic-gate 	long sff;
437*0Sstevel@tonic-gate 	SMDB_USER_INFO *user_info;
438*0Sstevel@tonic-gate 	struct stat *stat_info;
439*0Sstevel@tonic-gate {
440*0Sstevel@tonic-gate 	int st;
441*0Sstevel@tonic-gate 	int result;
442*0Sstevel@tonic-gate 	char db_file_name[MAXPATHLEN];
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
445*0Sstevel@tonic-gate 				    extension);
446*0Sstevel@tonic-gate 	if (result != SMDBE_OK)
447*0Sstevel@tonic-gate 		return result;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	st = safefile(db_file_name, user_info->smdbu_id,
450*0Sstevel@tonic-gate 		      user_info->smdbu_group_id, user_info->smdbu_name,
451*0Sstevel@tonic-gate 		      sff, mode_mask, stat_info);
452*0Sstevel@tonic-gate 	if (st != 0)
453*0Sstevel@tonic-gate 		return st;
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	return SMDBE_OK;
456*0Sstevel@tonic-gate }
457*0Sstevel@tonic-gate /*
458*0Sstevel@tonic-gate **  SMDB_FILECHANGED -- Checks to see if a file changed.
459*0Sstevel@tonic-gate **
460*0Sstevel@tonic-gate **	Compares the passed in stat_info with a current stat on
461*0Sstevel@tonic-gate **	the passed in file descriptor. Check filechanged for
462*0Sstevel@tonic-gate **	return values.
463*0Sstevel@tonic-gate **
464*0Sstevel@tonic-gate **	Parameters:
465*0Sstevel@tonic-gate **		db_name -- The name of the database without extension.
466*0Sstevel@tonic-gate **		extension -- The extension.
467*0Sstevel@tonic-gate **		db_fd -- A file descriptor for the database file.
468*0Sstevel@tonic-gate **		stat_info -- An old stat_info.
469*0Sstevel@tonic-gate **	Returns:
470*0Sstevel@tonic-gate **		SMDBE_OK -- Success, otherwise errno.
471*0Sstevel@tonic-gate */
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate int
smdb_filechanged(db_name,extension,db_fd,stat_info)474*0Sstevel@tonic-gate smdb_filechanged(db_name, extension, db_fd, stat_info)
475*0Sstevel@tonic-gate 	char *db_name;
476*0Sstevel@tonic-gate 	char *extension;
477*0Sstevel@tonic-gate 	int db_fd;
478*0Sstevel@tonic-gate 	struct stat *stat_info;
479*0Sstevel@tonic-gate {
480*0Sstevel@tonic-gate 	int result;
481*0Sstevel@tonic-gate 	char db_file_name[MAXPATHLEN];
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
484*0Sstevel@tonic-gate 				    extension);
485*0Sstevel@tonic-gate 	if (result != SMDBE_OK)
486*0Sstevel@tonic-gate 		return result;
487*0Sstevel@tonic-gate 	return filechanged(db_file_name, db_fd, stat_info);
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate /*
490*0Sstevel@tonic-gate ** SMDB_PRINT_AVAILABLE_TYPES -- Prints the names of the available types.
491*0Sstevel@tonic-gate **
492*0Sstevel@tonic-gate **	Parameters:
493*0Sstevel@tonic-gate **		None
494*0Sstevel@tonic-gate **
495*0Sstevel@tonic-gate **	Returns:
496*0Sstevel@tonic-gate **		None
497*0Sstevel@tonic-gate */
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate void
smdb_print_available_types()500*0Sstevel@tonic-gate smdb_print_available_types()
501*0Sstevel@tonic-gate {
502*0Sstevel@tonic-gate #ifdef NDBM
503*0Sstevel@tonic-gate 	printf("dbm\n");
504*0Sstevel@tonic-gate #endif /* NDBM */
505*0Sstevel@tonic-gate #ifdef NEWDB
506*0Sstevel@tonic-gate 	printf("hash\n");
507*0Sstevel@tonic-gate 	printf("btree\n");
508*0Sstevel@tonic-gate #endif /* NEWDB */
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate /*
511*0Sstevel@tonic-gate ** SMDB_DB_DEFINITION -- Given a database type, return database definition
512*0Sstevel@tonic-gate **
513*0Sstevel@tonic-gate **	Reads though a structure making an association with the database
514*0Sstevel@tonic-gate **	type and the required cpp define from sendmail/README.
515*0Sstevel@tonic-gate **	List size is dynamic and must be NULL terminated.
516*0Sstevel@tonic-gate **
517*0Sstevel@tonic-gate **	Parameters:
518*0Sstevel@tonic-gate **		type -- The name of the database type.
519*0Sstevel@tonic-gate **
520*0Sstevel@tonic-gate **	Returns:
521*0Sstevel@tonic-gate **		definition for type, otherwise NULL.
522*0Sstevel@tonic-gate */
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate typedef struct
525*0Sstevel@tonic-gate {
526*0Sstevel@tonic-gate 	SMDB_DBTYPE type;
527*0Sstevel@tonic-gate 	char *dbdef;
528*0Sstevel@tonic-gate } dbtype;
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate static dbtype DatabaseDefs[] =
531*0Sstevel@tonic-gate {
532*0Sstevel@tonic-gate 	{ SMDB_TYPE_HASH,	"NEWDB" },
533*0Sstevel@tonic-gate 	{ SMDB_TYPE_BTREE,	"NEWDB" },
534*0Sstevel@tonic-gate 	{ SMDB_TYPE_NDBM,	"NDBM"	},
535*0Sstevel@tonic-gate 	{ NULL,			"OOPS"	}
536*0Sstevel@tonic-gate };
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate char *
smdb_db_definition(type)539*0Sstevel@tonic-gate smdb_db_definition(type)
540*0Sstevel@tonic-gate 	SMDB_DBTYPE type;
541*0Sstevel@tonic-gate {
542*0Sstevel@tonic-gate 	dbtype *ptr = DatabaseDefs;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	while (ptr != NULL && ptr->type != NULL)
545*0Sstevel@tonic-gate 	{
546*0Sstevel@tonic-gate 		if (strcmp(type, ptr->type) == 0)
547*0Sstevel@tonic-gate 			return ptr->dbdef;
548*0Sstevel@tonic-gate 		ptr++;
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 	return NULL;
551*0Sstevel@tonic-gate }
552