summaryrefslogtreecommitdiff
path: root/lib/yp_db.c
blob: c98ec4ac70689ebe15263ac22537ed893a541485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* Copyright (c)  2000, 2001, 2002, 2003, 2004, 2009, 2011 Thorsten Kukuk
   Author: Thorsten Kukuk <kukuk@suse.de>

   The YP Server is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   version 2 as published by the Free Software Foundation.

   The YP Server is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with the YP Server; see the file COPYING. If
   not, write to the Free Software Foundation, Inc., 51 Franklin Street,
   Suite 500, Boston, MA 02110-1335, USA. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/param.h>

#include "ypserv_conf.h"
#include "log_msg.h"
#include "yp_db.h"
#include "yp.h"

#if defined(HAVE_LIBGDBM)
#include <gdbm.h>
#elif defined(HAVE_LIBQDBM)
#include <hovel.h>
#elif defined(HAVE_NDBM)
#include <ndbm.h>
#elif defined(HAVE_LIBTC)
#include <tcbdb.h>
#endif

#if defined(HAVE_COMPAT_LIBGDBM)

/* Open a GDBM database */
static GDBM_FILE
_db_open (const char *domain, const char *map)
{
  GDBM_FILE dbp;
  char buf[MAXPATHLEN + 2];

  if (strlen (domain) + strlen (map) < MAXPATHLEN)
    {
      sprintf (buf, "%s/%s", domain, map);

      dbp = gdbm_open (buf, 0, GDBM_READER, 0, NULL);

      if (debug_flag && dbp == NULL)
	log_msg ("gdbm_open: GDBM Error Code #%d", gdbm_errno);
      else if (debug_flag)
	log_msg ("\t\t->Returning OK!");
    }
  else
    {
      dbp = NULL;
      log_msg ("Path to long: %s/%s", domain, map);
    }

  return dbp;
}

static inline int
_db_close (GDBM_FILE file)
{
  gdbm_close (file);
  return 0;
}

#elif defined(HAVE_NDBM)

/*****************************************************
  The following stuff is for NDBM suport !
******************************************************/

/* Open a NDBM database */
static DB_FILE
_db_open (const char *domain, const char *map)
{
  DB_FILE dbp;
  char buf[MAXPATHLEN + 2];

  if (strlen (domain) + strlen (map) < MAXPATHLEN)
    {
      sprintf (buf, "%s/%s", domain, map);

      dbp = dbm_open (buf, O_RDONLY, 0600);

      if (debug_flag && dbp == NULL)
	log_msg ("dbm_open: NDBM Error Code #%d", errno);
      else if (debug_flag)
	log_msg ("\t\t->Returning OK!");
    }
  else
    {
      dbp = NULL;
      log_msg ("Path to long: %s/%s", domain, map);
    }

  return dbp;
}

static inline int
_db_close (DB_FILE file)
{
  dbm_close (file);
  return 0;
}

int
ypdb_exists (DB_FILE dbp, datum key)
{
  datum tmp = dbm_fetch (dbp, key);

  if (tmp.dptr != NULL)
    return 1;
  else
    return 0;
}

datum
ypdb_nextkey (DB_FILE file, datum key)
{
  datum tkey;

  tkey = dbm_firstkey (file);
  while ((key.dsize != tkey.dsize) ||
	   (strncmp (key.dptr, tkey.dptr, tkey.dsize) != 0))
    {
      tkey = dbm_nextkey (file);
      if (tkey.dptr == NULL)
	return tkey;
    }
  tkey = dbm_nextkey (file);

  return tkey;
}

#elif defined(HAVE_LIBTC)

/*****************************************************
  The following stuff is for Tokyo Cabinet suport !
******************************************************/

/* Open a Tokyo Cabinet B+ Tree database */
static DB_FILE
_db_open (const char *domain, const char *map)
{
  DB_FILE dbp;
  char buf[MAXPATHLEN + 2];
  int isok;

  if (strlen (domain) + strlen (map) < MAXPATHLEN)
    {
      sprintf (buf, "%s/%s", domain, map);

      dbp = tcbdbnew ();
      isok = tcbdbopen (dbp, buf, BDBOREADER | BDBONOLCK);

      if (debug_flag && !isok)
        {
      	  log_msg ("tcbdbopen: Tokyo Cabinet Error: %s", 
                   tcbdberrmsg (tcbdbecode (dbp)));
      	  log_msg ("tcbdbopen: consider rebuilding maps using ypinit");
      	}
      else if (debug_flag)
	log_msg ("\t\t->Returning OK!");
      if ( !isok )
	{
	  /* DB not successful opened. Close database object and set return value to NULL. */
	  tcbdbdel (dbp);
	  dbp = NULL;
	}
    }
  else
    {
      dbp = NULL;
      log_msg ("Path too long: %s/%s", domain, map);
    }

  return dbp;
}

static inline int
_db_close (DB_FILE dbp)
{
  tcbdbclose (dbp);
  tcbdbdel (dbp);
  dbp = NULL;
  return 0;
}

datum
ypdb_firstkey (DB_FILE dbp)
{
  datum tkey;
  BDBCUR *cur;
  
  /* In case of error, we return original key */
  if (!(cur = tcbdbcurnew (dbp)) || !tcbdbcurfirst (cur)
      || (tkey.dptr = tcbdbcurkey (cur, &tkey.dsize)) == NULL)
    {
      tkey.dptr = NULL;
      tkey.dsize = 0;
    }

  if (cur)
    tcbdbcurdel (cur);

  return tkey;
}

int
ypdb_exists (DB_FILE dbp, datum key)
{
  return tcbdbvnum (dbp, key.dptr, key.dsize) > 0;
}

datum
ypdb_nextkey (DB_FILE dbp, datum key)
{
  datum tkey;
  BDBCUR *cur;
  
  tkey.dptr = NULL;
  tkey.dsize = 0;
  
  /* In case of error, we return empty key */
  if (!(cur = tcbdbcurnew (dbp)))
    return tkey;

  /* We try to jump to key and get next, or we move to the first */
  if (tcbdbcurjump (cur, key.dptr, key.dsize) && tcbdbcurnext (cur))
    {
      if ((tkey.dptr = tcbdbcurkey (cur, &tkey.dsize)) == NULL)
        tkey.dsize = 0;
    }
    
  tcbdbcurdel (cur);
  return tkey;
}

datum
ypdb_fetch (DB_FILE bdb, datum key)
{
  datum res;
  
  if ((res.dptr = tcbdbget (bdb, key.dptr, key.dsize, &res.dsize)) == NULL)
      res.dsize = 0;
  
  return res;
}

#else

#error "No database found or selected!"

#endif

typedef struct _fopen
{
  char *domain;
  char *map;
  DB_FILE dbp;
  int flag;
}
Fopen, *FopenP;

#define F_OPEN_FLAG 1
#define F_MUST_CLOSE 2

static int fast_open_init = -1;
static Fopen fast_open_files[255];

int
ypdb_close_all (void)
{
  int i;

  if (debug_flag)
    log_msg ("ypdb_close_all() called");

  if (fast_open_init == -1)
    return 0;

  for (i = 0; i < cached_filehandles; i++)
    {
      if (fast_open_files[i].dbp != NULL)
	{
	  if (fast_open_files[i].flag & F_OPEN_FLAG)
	    {
	      if (debug_flag)
		log_msg ("ypdb_close_all (%s/%s|%d) MARKED_TO_BE_CLOSE",
			 fast_open_files[i].domain,
			 fast_open_files[i].map, i);
	      fast_open_files[i].flag |= F_MUST_CLOSE;
	    }
	  else
	    {
	      if (debug_flag)
		log_msg ("ypdb_close_all (%s/%s|%d)",
			 fast_open_files[i].domain,
			 fast_open_files[i].map, i);
	      free (fast_open_files[i].domain);
	      fast_open_files[i].domain = NULL;
	      free (fast_open_files[i].map);
	      fast_open_files[i].map = NULL;
	      _db_close (fast_open_files[i].dbp);
	      fast_open_files[i].dbp = NULL;
	      fast_open_files[i].flag = 0;
	    }
	}
    }

  return 0;
}

int
ypdb_close (DB_FILE file)
{
  if (debug_flag)
    log_msg ("ypdb_close() called");

  if (cached_filehandles > 0)
    {
      if (fast_open_init != -1)
	{
	  int i;

	  for (i = 0; i < cached_filehandles; ++i)
	    {
	      if (fast_open_files[i].dbp == file)
		{
		  if (fast_open_files[i].flag & F_MUST_CLOSE)
		    {
		      if (debug_flag)
			log_msg ("ypdb_MUST_close (%s/%s|%d)",
				 fast_open_files[i].domain,
				 fast_open_files[i].map, i);
		      free (fast_open_files[i].domain);
		      fast_open_files[i].domain = NULL;
		      free (fast_open_files[i].map);
		      fast_open_files[i].map = NULL;
		      _db_close (fast_open_files[i].dbp);
		      fast_open_files[i].dbp = NULL;
		      fast_open_files[i].flag = 0;
		    }
		  else
		    {
		      fast_open_files[i].flag &= ~F_OPEN_FLAG;
		    }
		  return 0;
		}
	    }
	}
      log_msg ("ERROR: Could not close file!");
      return 1;
    }
  else
    {
      _db_close (file);
      return 0;
    }
}

DB_FILE
ypdb_open (const char *domain, const char *map)
{
  int i;

  if (debug_flag)
    log_msg ("\typdb_open(\"%s\", \"%s\")", domain, map);

  if (map[0] == '.' || strchr (map, '/'))
    {
      if (debug_flag)
	log_msg ("\t\t->Returning 0");
      return NULL;
    }

  if (cached_filehandles > 0)
    {
      /* First call, initialize the fast_open_init struct */
      if (fast_open_init == -1)
	{
	  fast_open_init = 0;
	  for (i = 0; i < cached_filehandles; i++)
	    {
	      fast_open_files[i].domain =
		fast_open_files[i].map = NULL;
	      fast_open_files[i].dbp = (DB_FILE) NULL;
	      fast_open_files[i].flag = 0;
	    }
	}

      /* Search if we have already open the domain/map file */
      for (i = 0; i < cached_filehandles; i++)
	{
	  if (fast_open_files[i].dbp != NULL)
	    {
	      if ((strcmp (domain, fast_open_files[i].domain) == 0) &&
		  (strcmp (map, fast_open_files[i].map) == 0))
		{
		  /* The file is open and we know the file handle */
		  if (debug_flag)
		    log_msg ("Found: %s/%s (%d)", fast_open_files[i].domain,
			     fast_open_files[i].map, i);

		  if (fast_open_files[i].flag & F_OPEN_FLAG)
		    {
		      /* The file is already in use, don't open it twice.
			 I think this could never happen. */
		      log_msg ("\t%s/%s already open.", domain, map);
		      return NULL;
		    }
		  else
		    {
		      /* Mark the file as open */
		      fast_open_files[i].flag |= F_OPEN_FLAG;
		      return fast_open_files[i].dbp;
		    }
		}
	    }
	}

      /* Search for free entry. If we do not found one, close the LRU */
      for (i = 0; i < cached_filehandles; i++)
	{
#if 0
	  /* Bad Idea. If one of them is NULL, we will get a seg.fault
	     I think it will only work with Linux libc 5.x */
	  log_msg ("Opening: %s/%s (%d) %x",
		   fast_open_files[i].domain,
		   fast_open_files[i].map,
		   i, fast_open_files[i].dbp);
#endif
	  if (fast_open_files[i].dbp == NULL)
	    {
	      /* Good, we have a free entry and don't need to close a map */
	      int j;
	      Fopen tmp;

	      if ((fast_open_files[i].dbp = _db_open (domain, map)) == NULL)
		return NULL;
	      /* since .dbp is NULL, .domain must be NULL, too */
	      assert (fast_open_files[i].domain == NULL);
	      fast_open_files[i].domain = strdup (domain);
	      /* since .dbp is NULL, .map must be NULL, too */
	      assert (fast_open_files[i].map == NULL);
	      fast_open_files[i].map = strdup (map);
	      fast_open_files[i].flag |= F_OPEN_FLAG;

	      if (debug_flag)
		log_msg ("Opening: %s/%s (%d) %x", domain, map, i,
			 fast_open_files[i].dbp);

	      /* LRU: put this entry at the first position, move all the other
		 one back */
	      tmp = fast_open_files[i];
	      for (j = i; j >= 1; --j)
		fast_open_files[j] = fast_open_files[j-1];

	      fast_open_files[0] = tmp;
	      return fast_open_files[0].dbp;
	    }
	}

      /* The badest thing, which could happen: no free cache entrys.
	 Search the last entry, which isn't in use. */
      for (i = (cached_filehandles - 1); i > 0; --i)
	if ((fast_open_files[i].flag & F_OPEN_FLAG) != F_OPEN_FLAG)
	  {
	    int j;
	    Fopen tmp;
	    DB_FILE dbp;

	    /* Check, if we can open the file. Else there is no reason
	       to close a cached handle.  */
	    if ((dbp = _db_open (domain, map)) == NULL)
	      return NULL;

	    if (debug_flag)
	      {
		log_msg ("Closing %s/%s (%d)",
			 fast_open_files[i].domain,
			 fast_open_files[i].map, i);
		log_msg ("Opening: %s/%s (%d)", domain, map, i);
	      }
	    free (fast_open_files[i].domain);
	    free (fast_open_files[i].map);
	    _db_close (fast_open_files[i].dbp);

	    fast_open_files[i].domain = strdup (domain);
	    fast_open_files[i].map = strdup (map);
	    fast_open_files[i].flag = F_OPEN_FLAG;
	    fast_open_files[i].dbp = dbp;

	    /* LRU: Move the new entry to the first positon */
	    tmp = fast_open_files[i];
	    for (j = i; j >= 1; --j)
	      fast_open_files[j] = fast_open_files[j-1];

	    fast_open_files[j] = tmp;
	    return fast_open_files[j].dbp;
	  }

      log_msg ("ERROR: Couldn't find a free cache entry!");

      for (i = 0; i < cached_filehandles; i++)
	{
	  log_msg ("Open files: %s/%s (%d) %x (%x)",
		   fast_open_files[i].domain,
		   fast_open_files[i].map,
		   i, fast_open_files[i].dbp,
		   fast_open_files[i].flag);
	}
      return NULL;
    }
  else
    return _db_open (domain, map);
}