1*0438cf0aSkrw /* $OpenBSD: db.c,v 1.18 2017/02/13 23:04:05 krw Exp $ */
2e853bc5dShenning
38052c184Shenning /*
48052c184Shenning * Persistent database management routines for DHCPD.
58052c184Shenning */
6e853bc5dShenning
7e853bc5dShenning /*
8e853bc5dShenning * Copyright (c) 1995, 1996 The Internet Software Consortium.
9e853bc5dShenning * All rights reserved.
10e853bc5dShenning *
11e853bc5dShenning * Redistribution and use in source and binary forms, with or without
12e853bc5dShenning * modification, are permitted provided that the following conditions
13e853bc5dShenning * are met:
14e853bc5dShenning *
15e853bc5dShenning * 1. Redistributions of source code must retain the above copyright
16e853bc5dShenning * notice, this list of conditions and the following disclaimer.
17e853bc5dShenning * 2. Redistributions in binary form must reproduce the above copyright
18e853bc5dShenning * notice, this list of conditions and the following disclaimer in the
19e853bc5dShenning * documentation and/or other materials provided with the distribution.
20e853bc5dShenning * 3. Neither the name of The Internet Software Consortium nor the names
21e853bc5dShenning * of its contributors may be used to endorse or promote products derived
22e853bc5dShenning * from this software without specific prior written permission.
23e853bc5dShenning *
24e853bc5dShenning * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25e853bc5dShenning * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26e853bc5dShenning * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27e853bc5dShenning * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28e853bc5dShenning * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29e853bc5dShenning * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30e853bc5dShenning * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31e853bc5dShenning * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32e853bc5dShenning * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33e853bc5dShenning * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34e853bc5dShenning * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35e853bc5dShenning * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36e853bc5dShenning * SUCH DAMAGE.
37e853bc5dShenning *
38e853bc5dShenning * This software has been written for the Internet Software Consortium
39e853bc5dShenning * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40e853bc5dShenning * Enterprises. To learn more about the Internet Software Consortium,
41e853bc5dShenning * see ``http://www.vix.com/isc''. To learn more about Vixie
42e853bc5dShenning * Enterprises, see ``http://www.vix.com''.
43e853bc5dShenning */
44e853bc5dShenning
45837cddffSkrw #include <sys/types.h>
46837cddffSkrw #include <sys/socket.h>
47837cddffSkrw
48837cddffSkrw #include <net/if.h>
49837cddffSkrw
50837cddffSkrw #include <netinet/in.h>
51837cddffSkrw
52837cddffSkrw #include <fcntl.h>
53837cddffSkrw #include <stdio.h>
54579e3f2dSguenther #include <time.h>
55837cddffSkrw #include <unistd.h>
56837cddffSkrw
57837cddffSkrw #include "dhcp.h"
58837cddffSkrw #include "tree.h"
59e853bc5dShenning #include "dhcpd.h"
60c525a185Skrw #include "log.h"
61e853bc5dShenning
62e853bc5dShenning FILE *db_file;
63e853bc5dShenning
64e853bc5dShenning static int counting = 0;
65e853bc5dShenning static int count = 0;
66fbc5e94dShenning time_t write_time;
67e853bc5dShenning
688052c184Shenning /*
698052c184Shenning * Write the specified lease to the current lease database file.
708052c184Shenning */
718052c184Shenning int
write_lease(struct lease * lease)728052c184Shenning write_lease(struct lease *lease)
73e853bc5dShenning {
74ca617185Skrw char tbuf[26]; /* "w yyyy/mm/dd hh:mm:ss UTC" */
75ca617185Skrw size_t rsltsz;
76e853bc5dShenning int errors = 0;
77e853bc5dShenning int i;
78e853bc5dShenning
79e853bc5dShenning if (counting)
80e853bc5dShenning ++count;
817f157d3aSkrw if (fprintf(db_file, "lease %s {\n", piaddr(lease->ip_addr)) == -1)
82e853bc5dShenning ++errors;
83e853bc5dShenning
84ca617185Skrw rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT,
85ca617185Skrw gmtime(&lease->starts));
86ca617185Skrw if (rsltsz == 0 || fprintf(db_file, "\tstarts %s;\n", tbuf) == -1)
87ca617185Skrw errors++;
888052c184Shenning
89ca617185Skrw rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT,
90ca617185Skrw gmtime(&lease->ends));
91ca617185Skrw if (rsltsz == 0 || fprintf(db_file, "\tends %s;\n", tbuf) == -1)
92ca617185Skrw errors++;
93e853bc5dShenning
94e853bc5dShenning if (lease->hardware_addr.hlen) {
958eae277cSkrw if (fprintf(db_file, "\thardware %s %s;",
96e853bc5dShenning hardware_types[lease->hardware_addr.htype],
97e853bc5dShenning print_hw_addr(lease->hardware_addr.htype,
98e853bc5dShenning lease->hardware_addr.hlen,
997f157d3aSkrw lease->hardware_addr.haddr)) == -1)
100e853bc5dShenning ++errors;
101e853bc5dShenning }
1028052c184Shenning
103e853bc5dShenning if (lease->uid_len) {
1040795b389Sderaadt int j;
1050795b389Sderaadt
1067f157d3aSkrw if (fprintf(db_file, "\n\tuid %2.2x", lease->uid[0]) == -1)
107e853bc5dShenning ++errors;
1088052c184Shenning
1090795b389Sderaadt for (j = 1; j < lease->uid_len; j++) {
1107f157d3aSkrw if (fprintf(db_file, ":%2.2x", lease->uid[j]) == -1)
111e853bc5dShenning ++errors;
112e853bc5dShenning }
1137f157d3aSkrw if (fputc(';', db_file) == EOF)
1147f157d3aSkrw ++errors;
115e853bc5dShenning }
1168052c184Shenning
117e853bc5dShenning if (lease->flags & BOOTP_LEASE) {
1187f157d3aSkrw if (fprintf(db_file, "\n\tdynamic-bootp;") == -1)
119e853bc5dShenning ++errors;
120e853bc5dShenning }
1218052c184Shenning
122e853bc5dShenning if (lease->flags & ABANDONED_LEASE) {
1237f157d3aSkrw if (fprintf(db_file, "\n\tabandoned;") == -1)
124e853bc5dShenning ++errors;
125e853bc5dShenning }
1268052c184Shenning
127e853bc5dShenning if (lease->client_hostname) {
128e853bc5dShenning for (i = 0; lease->client_hostname[i]; i++)
129e853bc5dShenning if (lease->client_hostname[i] < 33 ||
130e853bc5dShenning lease->client_hostname[i] > 126)
131e853bc5dShenning goto bad_client_hostname;
1327f157d3aSkrw if (fprintf(db_file, "\n\tclient-hostname \"%s\";",
1337f157d3aSkrw lease->client_hostname) == -1)
134e853bc5dShenning ++errors;
135e853bc5dShenning }
1368052c184Shenning
137e853bc5dShenning bad_client_hostname:
138e853bc5dShenning if (lease->hostname) {
139e853bc5dShenning for (i = 0; lease->hostname[i]; i++)
140e853bc5dShenning if (lease->hostname[i] < 33 ||
141e853bc5dShenning lease->hostname[i] > 126)
142e853bc5dShenning goto bad_hostname;
1437f157d3aSkrw if (fprintf(db_file, "\n\thostname \"%s\";",
1447f157d3aSkrw lease->hostname) == -1)
145e853bc5dShenning ++errors;
146e853bc5dShenning }
1478052c184Shenning
148e853bc5dShenning bad_hostname:
1497f157d3aSkrw if (fputs("\n}\n", db_file) == EOF)
150e853bc5dShenning ++errors;
1518052c184Shenning
152e853bc5dShenning if (errors)
153c525a185Skrw log_info("write_lease: unable to write lease %s",
154e853bc5dShenning piaddr(lease->ip_addr));
1558052c184Shenning
1568052c184Shenning return (!errors);
157e853bc5dShenning }
158e853bc5dShenning
1598052c184Shenning /*
1608052c184Shenning * Commit any leases that have been written out...
1618052c184Shenning */
1628052c184Shenning int
commit_leases(void)1638052c184Shenning commit_leases(void)
164e853bc5dShenning {
1658052c184Shenning /*
1668052c184Shenning * Commit any outstanding writes to the lease database file. We need to
1678052c184Shenning * do this even if we're rewriting the file below, just in case the
1688052c184Shenning * rewrite fails.
1698052c184Shenning */
170e853bc5dShenning if (fflush(db_file) == EOF) {
171*0438cf0aSkrw log_warn("commit_leases: unable to commit");
1728052c184Shenning return (0);
173e853bc5dShenning }
174e853bc5dShenning
1758052c184Shenning if (fsync(fileno(db_file)) == -1) {
176*0438cf0aSkrw log_warn("commit_leases: unable to commit");
1778052c184Shenning return (0);
1788052c184Shenning }
1798052c184Shenning
1808052c184Shenning /*
1818052c184Shenning * If we've written more than a thousand leases or if we haven't
1828052c184Shenning * rewritten the lease database in over an hour, rewrite it now.
1838052c184Shenning */
184e853bc5dShenning if (count > 1000 || (count && cur_time - write_time > 3600)) {
185e853bc5dShenning count = 0;
186e853bc5dShenning write_time = cur_time;
187e853bc5dShenning new_lease_file();
188e853bc5dShenning }
1898052c184Shenning
1908052c184Shenning return (1);
191e853bc5dShenning }
192e853bc5dShenning
1938052c184Shenning void
db_startup(void)1948052c184Shenning db_startup(void)
195e853bc5dShenning {
1966d74e8b9Shenning int db_fd;
1976d74e8b9Shenning
1986d74e8b9Shenning /* open lease file. once we dropped privs it has to stay open */
199328fd5c9Skrw db_fd = open(path_dhcpd_db, O_WRONLY|O_CREAT, 0640);
2006d74e8b9Shenning if (db_fd == -1)
201*0438cf0aSkrw fatal("Can't create new lease file");
2026d74e8b9Shenning if ((db_file = fdopen(db_fd, "w")) == NULL)
203c525a185Skrw fatalx("Can't fdopen new lease file!");
2046d74e8b9Shenning
205e853bc5dShenning /* Read in the existing lease file... */
206e853bc5dShenning read_leases();
207fbc5e94dShenning time(&write_time);
2086d74e8b9Shenning
209e853bc5dShenning new_lease_file();
210e853bc5dShenning }
211e853bc5dShenning
2128052c184Shenning void
new_lease_file(void)2133dad9bb3Sderaadt new_lease_file(void)
214e853bc5dShenning {
2156d74e8b9Shenning fflush(db_file);
2166d74e8b9Shenning rewind(db_file);
2178052c184Shenning
218e853bc5dShenning /* Write out all the leases that we know of... */
219e853bc5dShenning counting = 0;
220e853bc5dShenning write_leases();
221e853bc5dShenning
2226d74e8b9Shenning fflush(db_file);
2230795b389Sderaadt ftruncate(fileno(db_file), ftello(db_file));
2246d74e8b9Shenning fsync(fileno(db_file));
225e853bc5dShenning
226e853bc5dShenning counting = 1;
227e853bc5dShenning }
228