1996-03-02 05:13:36 +00:00
|
|
|
/* db.c
|
|
|
|
|
|
|
|
Persistent database management routines for DHCPD... */
|
|
|
|
|
|
|
|
/*
|
1999-03-16 05:50:46 +00:00
|
|
|
* Copyright (c) 1996-1999 Internet Software Consortium.
|
|
|
|
* Use is subject to license terms which appear in the file named
|
|
|
|
* ISC-LICENSE that should have accompanied this file when you
|
|
|
|
* received it. If a file named ISC-LICENSE did not accompany this
|
|
|
|
* file, or you are not sure the one you have is correct, you may
|
|
|
|
* obtain an applicable copy of the license at:
|
1996-03-02 05:13:36 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* http://www.isc.org/isc-license-1.0.html.
|
1996-03-02 05:13:36 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* This file is part of the ISC DHCP distribution. The documentation
|
|
|
|
* associated with this file is listed in the file DOCUMENTATION,
|
|
|
|
* included in the top-level directory of this release.
|
1996-03-02 05:13:36 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* Support and other services are available for ISC products - see
|
|
|
|
* http://www.isc.org for more information.
|
1996-03-02 05:13:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char copyright[] =
|
2000-01-26 14:56:18 +00:00
|
|
|
"$Id: db.c,v 1.42 2000/01/26 14:56:18 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
|
1996-03-02 05:13:36 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "dhcpd.h"
|
1999-07-01 19:55:12 +00:00
|
|
|
#include <ctype.h>
|
1996-03-02 05:13:36 +00:00
|
|
|
|
|
|
|
FILE *db_file;
|
|
|
|
|
1996-06-11 08:13:28 +00:00
|
|
|
static int counting = 0;
|
|
|
|
static int count = 0;
|
|
|
|
TIME write_time;
|
|
|
|
|
1996-03-02 05:13:36 +00:00
|
|
|
/* Write the specified lease to the current lease database file. */
|
|
|
|
|
|
|
|
int write_lease (lease)
|
|
|
|
struct lease *lease;
|
|
|
|
{
|
|
|
|
struct tm *t;
|
|
|
|
char tbuf [64];
|
1996-08-27 09:42:26 +00:00
|
|
|
int errors = 0;
|
1998-08-05 19:32:20 +00:00
|
|
|
int i;
|
2000-01-08 01:46:54 +00:00
|
|
|
struct binding *b;
|
1996-03-02 05:13:36 +00:00
|
|
|
|
1996-06-11 08:13:28 +00:00
|
|
|
if (counting)
|
|
|
|
++count;
|
1996-03-02 05:13:36 +00:00
|
|
|
errno = 0;
|
1996-08-27 09:42:26 +00:00
|
|
|
fprintf (db_file, "lease %s {\n", piaddr (lease -> ip_addr));
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
|
1998-03-15 20:46:22 +00:00
|
|
|
/* Note: the following is not a Y2K bug - it's a Y1.9K bug. Until
|
|
|
|
somebody invents a time machine, I think we can safely disregard
|
|
|
|
it. */
|
1999-09-08 01:49:56 +00:00
|
|
|
if (lease -> starts != MAX_TIME) {
|
|
|
|
t = gmtime (&lease -> starts);
|
|
|
|
sprintf (tbuf, "%d %d/%02d/%02d %02d:%02d:%02d;",
|
|
|
|
t -> tm_wday, t -> tm_year + 1900,
|
|
|
|
t -> tm_mon + 1, t -> tm_mday,
|
|
|
|
t -> tm_hour, t -> tm_min, t -> tm_sec);
|
|
|
|
} else
|
1999-10-28 13:02:24 +00:00
|
|
|
strcpy (tbuf, "never;");
|
1996-08-27 09:42:26 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, " starts %s\n", tbuf);
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
|
1999-09-08 01:49:56 +00:00
|
|
|
if (lease -> ends != MAX_TIME) {
|
|
|
|
t = gmtime (&lease -> ends);
|
|
|
|
sprintf (tbuf, "%d %d/%02d/%02d %02d:%02d:%02d;",
|
|
|
|
t -> tm_wday, t -> tm_year + 1900,
|
|
|
|
t -> tm_mon + 1, t -> tm_mday,
|
|
|
|
t -> tm_hour, t -> tm_min, t -> tm_sec);
|
|
|
|
} else
|
1999-10-28 13:02:24 +00:00
|
|
|
strcpy (tbuf, "never;");
|
1996-08-27 09:42:26 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, " ends %s", tbuf);
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
|
2000-01-08 01:46:54 +00:00
|
|
|
if (lease -> tstp) {
|
|
|
|
t = gmtime (&lease -> tstp);
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n tstp %d %d/%02d/%02d %02d:%02d:%02d;",
|
2000-01-08 01:46:54 +00:00
|
|
|
t -> tm_wday, t -> tm_year + 1900,
|
|
|
|
t -> tm_mon + 1, t -> tm_mday,
|
|
|
|
t -> tm_hour, t -> tm_min, t -> tm_sec);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
2000-01-05 18:15:28 +00:00
|
|
|
}
|
2000-01-08 01:46:54 +00:00
|
|
|
if (lease -> tsfp) {
|
|
|
|
t = gmtime (&lease -> tsfp);
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n tsfp %d %d/%02d/%02d %02d:%02d:%02d;",
|
2000-01-08 01:46:54 +00:00
|
|
|
t -> tm_wday, t -> tm_year + 1900,
|
|
|
|
t -> tm_mon + 1, t -> tm_mday,
|
|
|
|
t -> tm_hour, t -> tm_min, t -> tm_sec);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
2000-01-05 18:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lease -> flags & PEER_IS_OWNER) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n peer is owner;");
|
2000-01-05 18:15:28 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-11 08:00:11 +00:00
|
|
|
/* If this lease is billed to a class and is still valid,
|
|
|
|
write it out. */
|
|
|
|
if (lease -> billing_class && lease -> ends > cur_time)
|
|
|
|
if (!write_billing_class (lease -> billing_class))
|
|
|
|
++errors;
|
|
|
|
|
1996-03-02 05:13:36 +00:00
|
|
|
if (lease -> hardware_addr.hlen) {
|
1996-08-27 09:42:26 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n hardware %s %s;",
|
2000-01-05 18:15:28 +00:00
|
|
|
hardware_types [lease -> hardware_addr.hbuf [0]],
|
|
|
|
print_hw_addr (lease -> hardware_addr.hbuf [0],
|
|
|
|
lease -> hardware_addr.hlen - 1,
|
|
|
|
&lease -> hardware_addr.hbuf [1]));
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|
|
|
|
if (lease -> uid_len) {
|
|
|
|
int i;
|
1999-10-07 06:36:35 +00:00
|
|
|
if (db_printable_len ((const char *)lease -> uid,
|
1999-10-07 02:14:10 +00:00
|
|
|
lease -> uid_len)) {
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n uid \"%*s\";",
|
1999-10-07 06:36:35 +00:00
|
|
|
lease -> uid_len, lease -> uid);
|
1999-09-28 23:57:47 +00:00
|
|
|
} else {
|
1996-08-27 09:42:26 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n uid %2.2x", lease -> uid [0]);
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1999-09-28 23:57:47 +00:00
|
|
|
for (i = 1; i < lease -> uid_len; i++) {
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, ":%2.2x", lease -> uid [i]);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc (';', db_file);
|
1996-08-27 09:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lease -> flags & BOOTP_LEASE) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n dynamic-bootp;");
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errno) {
|
1997-03-06 19:29:39 +00:00
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lease -> flags & ABANDONED_LEASE) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n abandoned;");
|
1997-03-06 19:29:39 +00:00
|
|
|
if (errno) {
|
1996-08-27 09:42:26 +00:00
|
|
|
++errors;
|
1997-05-09 08:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
2000-01-25 01:39:57 +00:00
|
|
|
for (b = lease -> scope.bindings; b; b = b -> next) {
|
|
|
|
if (b -> value.data) {
|
|
|
|
if (db_printable_len (b -> value.data,
|
|
|
|
b -> value.len)) {
|
2000-01-08 01:46:54 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n set %s = \"%.*s\";",
|
|
|
|
b -> name,
|
|
|
|
(int)b -> value.len, b -> value.data);
|
2000-01-08 01:46:54 +00:00
|
|
|
} else {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n set %s = ", b -> name);
|
2000-01-08 01:46:54 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
for (i = 0; i < b -> value.len; i++) {
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, "%2.2x%s",
|
|
|
|
b -> value.data [i],
|
|
|
|
i + 1 == b -> value.len
|
2000-01-25 01:39:57 +00:00
|
|
|
? "" : ":");
|
2000-01-08 01:46:54 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc (';', db_file);
|
|
|
|
}
|
2000-01-25 01:39:57 +00:00
|
|
|
}
|
1999-07-01 19:55:12 +00:00
|
|
|
}
|
|
|
|
if (lease -> client_hostname &&
|
|
|
|
db_printable (lease -> client_hostname)) {
|
1997-05-09 08:22:00 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n client-hostname \"%s\";",
|
1997-05-09 08:22:00 +00:00
|
|
|
lease -> client_hostname);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
1999-07-01 19:55:12 +00:00
|
|
|
if (lease -> hostname && db_printable (lease -> hostname)) {
|
1997-05-09 08:22:00 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n hostname \"%s\";",
|
1997-10-27 20:16:26 +00:00
|
|
|
lease -> hostname);
|
1997-05-09 08:22:00 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
1996-08-27 09:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
1999-09-22 01:45:57 +00:00
|
|
|
if (lease -> on_expiry) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n on expiry%s {",
|
2000-01-08 01:46:54 +00:00
|
|
|
lease -> on_expiry == lease -> on_release
|
2000-01-25 01:39:57 +00:00
|
|
|
? " or release" : "");
|
1999-09-22 01:45:57 +00:00
|
|
|
if (errno)
|
|
|
|
++errors;
|
2000-01-25 01:39:57 +00:00
|
|
|
write_statements (db_file, lease -> on_expiry, 4);
|
1999-09-22 01:45:57 +00:00
|
|
|
/* XXX */
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n }");
|
1999-09-22 01:45:57 +00:00
|
|
|
}
|
2000-01-08 01:46:54 +00:00
|
|
|
if (lease -> on_release && lease -> on_release != lease -> on_expiry) {
|
1999-09-22 01:45:57 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n on release {");
|
1999-09-22 01:45:57 +00:00
|
|
|
if (errno)
|
|
|
|
++errors;
|
2000-01-25 01:39:57 +00:00
|
|
|
write_statements (db_file, lease -> on_release, 4);
|
1999-09-22 01:45:57 +00:00
|
|
|
/* XXX */
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n }");
|
1999-09-22 01:45:57 +00:00
|
|
|
}
|
1996-08-27 09:42:26 +00:00
|
|
|
errno = 0;
|
|
|
|
fputs ("\n}\n", db_file);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|
1996-08-27 09:42:26 +00:00
|
|
|
if (errors)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_info ("write_lease: unable to write lease %s",
|
1996-08-27 09:42:26 +00:00
|
|
|
piaddr (lease -> ip_addr));
|
|
|
|
return !errors;
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
1999-09-08 01:49:56 +00:00
|
|
|
int write_host (host)
|
|
|
|
struct host_decl *host;
|
|
|
|
{
|
|
|
|
int errors = 0;
|
|
|
|
int i;
|
|
|
|
struct data_string ip_addrs;
|
|
|
|
|
|
|
|
if (!db_printable (host -> name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (counting)
|
|
|
|
++count;
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
fprintf (db_file, "host %s {", host -> name);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
|
1999-09-09 23:33:18 +00:00
|
|
|
if (host -> flags & HOST_DECL_DYNAMIC) {
|
1999-09-08 01:49:56 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n dynamic;");
|
1999-09-09 23:33:18 +00:00
|
|
|
if (errno)
|
1999-09-08 01:49:56 +00:00
|
|
|
++errors;
|
|
|
|
}
|
1999-09-09 23:33:18 +00:00
|
|
|
|
|
|
|
if (host -> flags & HOST_DECL_DELETED) {
|
1999-09-08 01:49:56 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n deleted;");
|
1999-09-09 23:33:18 +00:00
|
|
|
if (errno)
|
1999-09-08 01:49:56 +00:00
|
|
|
++errors;
|
1999-09-09 23:33:18 +00:00
|
|
|
} else {
|
|
|
|
if (host -> interface.hlen) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n hardware %s %s;",
|
2000-01-05 18:15:28 +00:00
|
|
|
hardware_types [host -> interface.hbuf [0]],
|
|
|
|
print_hw_addr (host -> interface.hbuf [0],
|
|
|
|
host -> interface.hlen - 1,
|
|
|
|
&host -> interface.hbuf [1]));
|
1999-09-09 23:33:18 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1999-09-08 01:49:56 +00:00
|
|
|
}
|
1999-09-09 23:33:18 +00:00
|
|
|
if (host -> client_identifier.len) {
|
|
|
|
int i;
|
1999-09-08 01:49:56 +00:00
|
|
|
errno = 0;
|
1999-10-07 06:36:35 +00:00
|
|
|
if (db_printable_len ((const char *)
|
1999-10-07 02:14:10 +00:00
|
|
|
host -> client_identifier.data,
|
1999-09-28 23:57:47 +00:00
|
|
|
host -> client_identifier.len)) {
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n uid \"%*s\";",
|
1999-09-28 23:57:47 +00:00
|
|
|
host -> client_identifier.len,
|
|
|
|
host -> client_identifier.data);
|
|
|
|
} else {
|
|
|
|
fprintf (db_file,
|
2000-01-25 01:39:57 +00:00
|
|
|
"\n uid %2.2x",
|
1999-09-28 23:57:47 +00:00
|
|
|
host -> client_identifier.data [0]);
|
1999-09-09 23:33:18 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1999-09-28 23:57:47 +00:00
|
|
|
for (i = 1;
|
|
|
|
i < host -> client_identifier.len; i++) {
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, ":%2.2x",
|
|
|
|
host ->
|
|
|
|
client_identifier.data [i]);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc (';', db_file);
|
1999-09-09 23:33:18 +00:00
|
|
|
}
|
1999-09-08 01:49:56 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 23:57:47 +00:00
|
|
|
memset (&ip_addrs, 0, sizeof ip_addrs);
|
1999-09-09 23:33:18 +00:00
|
|
|
if (host -> fixed_addr &&
|
|
|
|
evaluate_option_cache (&ip_addrs, (struct packet *)0,
|
|
|
|
(struct lease *)0,
|
|
|
|
(struct option_state *)0,
|
|
|
|
(struct option_state *)0,
|
2000-01-25 01:39:57 +00:00
|
|
|
&global_scope,
|
2000-01-26 14:56:18 +00:00
|
|
|
host -> fixed_addr, MDL)) {
|
1999-09-09 23:33:18 +00:00
|
|
|
|
1999-09-28 23:57:47 +00:00
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n fixed-address ");
|
1999-09-08 01:49:56 +00:00
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1999-09-09 23:33:18 +00:00
|
|
|
for (i = 0; i < ip_addrs.len - 3; i += 4) {
|
1999-09-28 23:57:47 +00:00
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, "%d.%d.%d.%d%s",
|
1999-09-09 23:33:18 +00:00
|
|
|
ip_addrs.data [i],
|
|
|
|
ip_addrs.data [i + 1],
|
|
|
|
ip_addrs.data [i + 2],
|
|
|
|
ip_addrs.data [i + 3],
|
|
|
|
i + 7 < ip_addrs.len ? "," : "");
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
1999-09-28 23:57:47 +00:00
|
|
|
errno = 0;
|
|
|
|
fputc (';', db_file);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host -> named_group) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n group \"%s\";",
|
1999-09-28 23:57:47 +00:00
|
|
|
host -> named_group -> name);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-04 23:19:36 +00:00
|
|
|
if (host -> group &&
|
|
|
|
(!host -> named_group ||
|
1999-10-05 02:44:37 +00:00
|
|
|
host -> group != host -> named_group -> group) &&
|
|
|
|
host -> group != &root_group) {
|
1999-09-28 23:57:47 +00:00
|
|
|
errno = 0;
|
|
|
|
write_statements (db_file,
|
|
|
|
host -> group -> statements, 8);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
1999-09-08 01:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
fputs ("\n}\n", db_file);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
if (errors)
|
|
|
|
log_info ("write_host: unable to write host %s",
|
|
|
|
host -> name);
|
|
|
|
return !errors;
|
|
|
|
}
|
|
|
|
|
1999-09-28 23:57:47 +00:00
|
|
|
int write_group (group)
|
|
|
|
struct group_object *group;
|
|
|
|
{
|
1999-10-01 03:24:59 +00:00
|
|
|
int errors = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!db_printable (group -> name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (counting)
|
|
|
|
++count;
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
fprintf (db_file, "group %s {", group -> name);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group -> flags & GROUP_OBJECT_DYNAMIC) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n dynamic;");
|
1999-10-01 03:24:59 +00:00
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group -> flags & GROUP_OBJECT_STATIC) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n static;");
|
1999-10-01 03:24:59 +00:00
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group -> flags & GROUP_OBJECT_DELETED) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n deleted;");
|
1999-10-01 03:24:59 +00:00
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
} else {
|
|
|
|
if (group -> group) {
|
|
|
|
errno = 0;
|
|
|
|
write_statements (db_file,
|
|
|
|
group -> group -> statements, 8);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
fputs ("\n}\n", db_file);
|
|
|
|
if (errno) {
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
if (errors)
|
|
|
|
log_info ("write_group: unable to write group %s",
|
|
|
|
group -> name);
|
|
|
|
return !errors;
|
1999-09-28 23:57:47 +00:00
|
|
|
}
|
|
|
|
|
1999-07-01 19:55:12 +00:00
|
|
|
int db_printable (s)
|
1999-10-07 06:36:35 +00:00
|
|
|
const char *s;
|
1999-07-01 19:55:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; s [i]; i++)
|
|
|
|
if (!isascii (s [i]) || !isprint (s [i]))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-09-28 23:57:47 +00:00
|
|
|
int db_printable_len (s, len)
|
1999-10-07 06:36:35 +00:00
|
|
|
const char *s;
|
|
|
|
unsigned len;
|
1999-09-28 23:57:47 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (!isascii (s [i]) || !isprint (s [i]))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-11-11 08:00:11 +00:00
|
|
|
/* Write a spawned class to the database file. */
|
|
|
|
|
|
|
|
int write_billing_class (class)
|
|
|
|
struct class *class;
|
|
|
|
{
|
|
|
|
int errors = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!class -> superclass) {
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n billing class \"%s\";", class -> name);
|
1998-11-11 08:00:11 +00:00
|
|
|
return !errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2000-01-25 01:39:57 +00:00
|
|
|
fprintf (db_file, "\n billing subclass \"%s\"",
|
1998-11-11 08:00:11 +00:00
|
|
|
class -> superclass -> name);
|
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
|
|
|
|
for (i = 0; i < class -> hash_string.len; i++)
|
|
|
|
if (!isascii (class -> hash_string.data [i]) ||
|
|
|
|
!isprint (class -> hash_string.data [i]))
|
|
|
|
break;
|
|
|
|
if (i == class -> hash_string.len) {
|
|
|
|
errno = 0;
|
1999-10-07 06:36:35 +00:00
|
|
|
fprintf (db_file, " \"%*s\";",
|
1998-11-11 08:00:11 +00:00
|
|
|
class -> hash_string.len,
|
|
|
|
class -> hash_string.data);
|
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
} else {
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, " %2.2x", class -> hash_string.data [0]);
|
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
for (i = 1; i < class -> hash_string.len; i++) {
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, ":%2.2x",
|
|
|
|
class -> hash_string.data [i]);
|
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
fprintf (db_file, ";");
|
|
|
|
if (errno)
|
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
class -> dirty = 0;
|
|
|
|
return !errors;
|
|
|
|
}
|
|
|
|
|
1996-03-02 05:13:36 +00:00
|
|
|
/* Commit any leases that have been written out... */
|
|
|
|
|
|
|
|
int commit_leases ()
|
|
|
|
{
|
1996-08-27 09:42:26 +00:00
|
|
|
/* Commit any outstanding writes to the lease database file.
|
|
|
|
We need to do this even if we're rewriting the file below,
|
|
|
|
just in case the rewrite fails. */
|
|
|
|
if (fflush (db_file) == EOF) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_info ("commit_leases: unable to commit: %m");
|
1996-08-27 09:42:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fsync (fileno (db_file)) < 0) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_info ("commit_leases: unable to commit: %m");
|
1996-08-27 09:42:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-06-11 08:13:28 +00:00
|
|
|
/* If we've written more than a thousand leases or if
|
|
|
|
we haven't rewritten the lease database in over an
|
|
|
|
hour, rewrite it now. */
|
|
|
|
if (count > 1000 || (count && cur_time - write_time > 3600)) {
|
|
|
|
count = 0;
|
|
|
|
write_time = cur_time;
|
|
|
|
new_lease_file ();
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-10-01 03:24:59 +00:00
|
|
|
void db_startup (testp)
|
|
|
|
int testp;
|
1996-03-02 05:13:36 +00:00
|
|
|
{
|
|
|
|
/* Read in the existing lease file... */
|
|
|
|
read_leases ();
|
|
|
|
|
1999-10-01 03:24:59 +00:00
|
|
|
if (!testp) {
|
1999-11-07 20:28:23 +00:00
|
|
|
db_file = fopen (path_dhcpd_db, "a");
|
1999-10-08 22:29:52 +00:00
|
|
|
expire_all_pools ();
|
1999-10-01 03:24:59 +00:00
|
|
|
GET_TIME (&write_time);
|
|
|
|
new_lease_file ();
|
|
|
|
}
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void new_lease_file ()
|
|
|
|
{
|
|
|
|
char newfname [512];
|
|
|
|
char backfname [512];
|
|
|
|
TIME t;
|
1997-12-02 07:44:56 +00:00
|
|
|
int db_fd;
|
1996-03-02 05:13:36 +00:00
|
|
|
|
|
|
|
/* If we already have an open database, close it. */
|
|
|
|
if (db_file) {
|
|
|
|
fclose (db_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a temporary lease file... */
|
1997-03-06 23:41:27 +00:00
|
|
|
GET_TIME (&t);
|
|
|
|
sprintf (newfname, "%s.%d", path_dhcpd_db, (int)t);
|
1997-12-02 07:44:56 +00:00
|
|
|
db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
|
|
|
|
if (db_fd < 0) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't create new lease file: %m");
|
1997-12-02 07:44:56 +00:00
|
|
|
}
|
|
|
|
if ((db_file = fdopen (db_fd, "w")) == NULL) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't fdopen new lease file!");
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
1999-04-23 22:30:56 +00:00
|
|
|
/* Write an introduction so people don't complain about time
|
|
|
|
being off. */
|
|
|
|
fprintf (db_file, "# All times in this file are in UTC (GMT), not %s",
|
|
|
|
"your local timezone. This is\n");
|
|
|
|
fprintf (db_file, "# not a bug, so please don't ask about it. %s",
|
1999-05-06 20:28:29 +00:00
|
|
|
"There is no portable way to\n");
|
|
|
|
fprintf (db_file, "# store leases in the local timezone, so please %s",
|
|
|
|
"don't request this as a\n");
|
|
|
|
fprintf (db_file, "# feature. If this is inconvenient or %s",
|
|
|
|
"confusing to you, we sincerely\n");
|
|
|
|
fprintf (db_file, "# apologize. Seriously, though - don't ask.\n");
|
|
|
|
fprintf (db_file, "# The format of this file is documented in the %s",
|
|
|
|
"dhcpd.leases(5) manual page.\n\n");
|
1999-04-23 22:30:56 +00:00
|
|
|
|
1996-03-02 05:13:36 +00:00
|
|
|
/* Write out all the leases that we know of... */
|
1996-06-11 08:13:28 +00:00
|
|
|
counting = 0;
|
1996-03-02 05:13:36 +00:00
|
|
|
write_leases ();
|
|
|
|
|
|
|
|
/* Get the old database out of the way... */
|
1996-09-02 21:16:25 +00:00
|
|
|
sprintf (backfname, "%s~", path_dhcpd_db);
|
1996-08-27 09:42:26 +00:00
|
|
|
if (unlink (backfname) < 0 && errno != ENOENT)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't remove old lease database backup %s: %m",
|
1996-08-27 09:42:26 +00:00
|
|
|
backfname);
|
1996-09-02 21:16:25 +00:00
|
|
|
if (link (path_dhcpd_db, backfname) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't backup lease database %s to %s: %m",
|
1996-09-02 21:16:25 +00:00
|
|
|
path_dhcpd_db, backfname);
|
1996-03-02 05:13:36 +00:00
|
|
|
|
|
|
|
/* Move in the new file... */
|
1996-09-02 21:16:25 +00:00
|
|
|
if (rename (newfname, path_dhcpd_db) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't install new lease database %s to %s: %m",
|
1996-09-02 21:16:25 +00:00
|
|
|
newfname, path_dhcpd_db);
|
1996-06-11 08:13:28 +00:00
|
|
|
|
|
|
|
counting = 1;
|
1996-03-02 05:13:36 +00:00
|
|
|
}
|