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