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