1 /* $OpenBSD: db.c,v 1.18 2017/02/13 23:04:05 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 <time.h> 55 #include <unistd.h> 56 57 #include "dhcp.h" 58 #include "tree.h" 59 #include "dhcpd.h" 60 #include "log.h" 61 62 FILE *db_file; 63 64 static int counting = 0; 65 static int count = 0; 66 time_t write_time; 67 68 /* 69 * Write the specified lease to the current lease database file. 70 */ 71 int 72 write_lease(struct lease *lease) 73 { 74 char tbuf[26]; /* "w yyyy/mm/dd hh:mm:ss UTC" */ 75 size_t rsltsz; 76 int errors = 0; 77 int i; 78 79 if (counting) 80 ++count; 81 if (fprintf(db_file, "lease %s {\n", piaddr(lease->ip_addr)) == -1) 82 ++errors; 83 84 rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT, 85 gmtime(&lease->starts)); 86 if (rsltsz == 0 || fprintf(db_file, "\tstarts %s;\n", tbuf) == -1) 87 errors++; 88 89 rsltsz = strftime(tbuf, sizeof(tbuf), DB_TIMEFMT, 90 gmtime(&lease->ends)); 91 if (rsltsz == 0 || fprintf(db_file, "\tends %s;\n", tbuf) == -1) 92 errors++; 93 94 if (lease->hardware_addr.hlen) { 95 if (fprintf(db_file, "\thardware %s %s;", 96 hardware_types[lease->hardware_addr.htype], 97 print_hw_addr(lease->hardware_addr.htype, 98 lease->hardware_addr.hlen, 99 lease->hardware_addr.haddr)) == -1) 100 ++errors; 101 } 102 103 if (lease->uid_len) { 104 int j; 105 106 if (fprintf(db_file, "\n\tuid %2.2x", lease->uid[0]) == -1) 107 ++errors; 108 109 for (j = 1; j < lease->uid_len; j++) { 110 if (fprintf(db_file, ":%2.2x", lease->uid[j]) == -1) 111 ++errors; 112 } 113 if (fputc(';', db_file) == EOF) 114 ++errors; 115 } 116 117 if (lease->flags & BOOTP_LEASE) { 118 if (fprintf(db_file, "\n\tdynamic-bootp;") == -1) 119 ++errors; 120 } 121 122 if (lease->flags & ABANDONED_LEASE) { 123 if (fprintf(db_file, "\n\tabandoned;") == -1) 124 ++errors; 125 } 126 127 if (lease->client_hostname) { 128 for (i = 0; lease->client_hostname[i]; i++) 129 if (lease->client_hostname[i] < 33 || 130 lease->client_hostname[i] > 126) 131 goto bad_client_hostname; 132 if (fprintf(db_file, "\n\tclient-hostname \"%s\";", 133 lease->client_hostname) == -1) 134 ++errors; 135 } 136 137 bad_client_hostname: 138 if (lease->hostname) { 139 for (i = 0; lease->hostname[i]; i++) 140 if (lease->hostname[i] < 33 || 141 lease->hostname[i] > 126) 142 goto bad_hostname; 143 if (fprintf(db_file, "\n\thostname \"%s\";", 144 lease->hostname) == -1) 145 ++errors; 146 } 147 148 bad_hostname: 149 if (fputs("\n}\n", db_file) == EOF) 150 ++errors; 151 152 if (errors) 153 log_info("write_lease: unable to write lease %s", 154 piaddr(lease->ip_addr)); 155 156 return (!errors); 157 } 158 159 /* 160 * Commit any leases that have been written out... 161 */ 162 int 163 commit_leases(void) 164 { 165 /* 166 * Commit any outstanding writes to the lease database file. We need to 167 * do this even if we're rewriting the file below, just in case the 168 * rewrite fails. 169 */ 170 if (fflush(db_file) == EOF) { 171 log_warn("commit_leases: unable to commit"); 172 return (0); 173 } 174 175 if (fsync(fileno(db_file)) == -1) { 176 log_warn("commit_leases: unable to commit"); 177 return (0); 178 } 179 180 /* 181 * If we've written more than a thousand leases or if we haven't 182 * rewritten the lease database in over an hour, rewrite it now. 183 */ 184 if (count > 1000 || (count && cur_time - write_time > 3600)) { 185 count = 0; 186 write_time = cur_time; 187 new_lease_file(); 188 } 189 190 return (1); 191 } 192 193 void 194 db_startup(void) 195 { 196 int db_fd; 197 198 /* open lease file. once we dropped privs it has to stay open */ 199 db_fd = open(path_dhcpd_db, O_WRONLY|O_CREAT, 0640); 200 if (db_fd == -1) 201 fatal("Can't create new lease file"); 202 if ((db_file = fdopen(db_fd, "w")) == NULL) 203 fatalx("Can't fdopen new lease file!"); 204 205 /* Read in the existing lease file... */ 206 read_leases(); 207 time(&write_time); 208 209 new_lease_file(); 210 } 211 212 void 213 new_lease_file(void) 214 { 215 fflush(db_file); 216 rewind(db_file); 217 218 /* Write out all the leases that we know of... */ 219 counting = 0; 220 write_leases(); 221 222 fflush(db_file); 223 ftruncate(fileno(db_file), ftello(db_file)); 224 fsync(fileno(db_file)); 225 226 counting = 1; 227 } 228