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