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