xref: /openbsd-src/usr.sbin/dhcpd/db.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: db.c,v 1.15 2016/02/06 23:50:10 krw Exp $	*/
2 
3 /*
4  * Persistent database management routines for DHCPD.
5  */
6 
7 /*
8  * Copyright (c) 1995, 1996 The Internet Software Consortium.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of The Internet Software Consortium nor the names
21  *    of its contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * This software has been written for the Internet Software Consortium
39  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40  * Enterprises.  To learn more about the Internet Software Consortium,
41  * see ``http://www.vix.com/isc''.  To learn more about Vixie
42  * Enterprises, see ``http://www.vix.com''.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 
48 #include <net/if.h>
49 
50 #include <netinet/in.h>
51 
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <unistd.h>
55 
56 #include "dhcp.h"
57 #include "tree.h"
58 #include "dhcpd.h"
59 
60 FILE *db_file;
61 
62 static int counting = 0;
63 static int count = 0;
64 time_t write_time;
65 
66 /*
67  * Write the specified lease to the current lease database file.
68  */
69 int
70 write_lease(struct lease *lease)
71 {
72 	char tbuf[26];	/* "w yyyy/mm/dd hh:mm:ss UTC" */
73 	size_t rsltsz;
74 	int errors = 0;
75 	int i;
76 
77 	if (counting)
78 		++count;
79 	if (fprintf(db_file, "lease %s {\n", piaddr(lease->ip_addr)) == -1)
80 		++errors;
81 
82 	rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT,
83 	    gmtime(&lease->starts));
84 	if (rsltsz == 0 || fprintf(db_file, "\tstarts %s;\n", tbuf) == -1)
85 		errors++;
86 
87 	rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT,
88 	    gmtime(&lease->ends));
89 	if (rsltsz == 0 || fprintf(db_file, "\tends %s;\n", tbuf) == -1)
90 		errors++;
91 
92 	if (lease->hardware_addr.hlen) {
93 		if (fprintf(db_file, "\thardware %s %s;",
94 		    hardware_types[lease->hardware_addr.htype],
95 		    print_hw_addr(lease->hardware_addr.htype,
96 		    lease->hardware_addr.hlen,
97 		    lease->hardware_addr.haddr)) == -1)
98 			++errors;
99 	}
100 
101 	if (lease->uid_len) {
102 		int j;
103 
104 		if (fprintf(db_file, "\n\tuid %2.2x", lease->uid[0]) == -1)
105 			++errors;
106 
107 		for (j = 1; j < lease->uid_len; j++) {
108 			if (fprintf(db_file, ":%2.2x", lease->uid[j]) == -1)
109 				++errors;
110 		}
111 		if (fputc(';', db_file) == EOF)
112 			++errors;
113 	}
114 
115 	if (lease->flags & BOOTP_LEASE) {
116 		if (fprintf(db_file, "\n\tdynamic-bootp;") == -1)
117 			++errors;
118 	}
119 
120 	if (lease->flags & ABANDONED_LEASE) {
121 		if (fprintf(db_file, "\n\tabandoned;") == -1)
122 			++errors;
123 	}
124 
125 	if (lease->client_hostname) {
126 		for (i = 0; lease->client_hostname[i]; i++)
127 			if (lease->client_hostname[i] < 33 ||
128 			    lease->client_hostname[i] > 126)
129 				goto bad_client_hostname;
130 		if (fprintf(db_file, "\n\tclient-hostname \"%s\";",
131 		    lease->client_hostname) == -1)
132 			++errors;
133 	}
134 
135 bad_client_hostname:
136 	if (lease->hostname) {
137 		for (i = 0; lease->hostname[i]; i++)
138 			if (lease->hostname[i] < 33 ||
139 			    lease->hostname[i] > 126)
140 				goto bad_hostname;
141 		if (fprintf(db_file, "\n\thostname \"%s\";",
142 		    lease->hostname) == -1)
143 			++errors;
144 	}
145 
146 bad_hostname:
147 	if (fputs("\n}\n", db_file) == EOF)
148 		++errors;
149 
150 	if (errors)
151 		note("write_lease: unable to write lease %s",
152 		    piaddr(lease->ip_addr));
153 
154 	return (!errors);
155 }
156 
157 /*
158  * Commit any leases that have been written out...
159  */
160 int
161 commit_leases(void)
162 {
163 	/*
164 	 * Commit any outstanding writes to the lease database file. We need to
165 	 * do this even if we're rewriting the file below, just in case the
166 	 * rewrite fails.
167 	 */
168 	if (fflush(db_file) == EOF) {
169 		note("commit_leases: unable to commit: %m");
170 		return (0);
171 	}
172 
173 	if (fsync(fileno(db_file)) == -1) {
174 		note("commit_leases: unable to commit: %m");
175 		return (0);
176 	}
177 
178 	/*
179 	 * If we've written more than a thousand leases or if we haven't
180 	 * rewritten the lease database in over an hour, rewrite it now.
181 	 */
182 	if (count > 1000 || (count && cur_time - write_time > 3600)) {
183 		count = 0;
184 		write_time = cur_time;
185 		new_lease_file();
186 	}
187 
188 	return (1);
189 }
190 
191 void
192 db_startup(void)
193 {
194 	int db_fd;
195 
196 	/* open lease file. once we dropped privs it has to stay open */
197 	db_fd = open(path_dhcpd_db, O_WRONLY|O_CREAT, 0640);
198 	if (db_fd == -1)
199 		error("Can't create new lease file: %m");
200 	if ((db_file = fdopen(db_fd, "w")) == NULL)
201 		error("Can't fdopen new lease file!");
202 
203 	/* Read in the existing lease file... */
204 	read_leases();
205 	time(&write_time);
206 
207 	new_lease_file();
208 }
209 
210 void
211 new_lease_file(void)
212 {
213 	fflush(db_file);
214 	rewind(db_file);
215 
216 	/* Write out all the leases that we know of... */
217 	counting = 0;
218 	write_leases();
219 
220 	fflush(db_file);
221 	ftruncate(fileno(db_file), ftello(db_file));
222 	fsync(fileno(db_file));
223 
224 	counting = 1;
225 }
226