1 /* $OpenBSD: db.c,v 1.10 2004/09/16 18:35:42 deraadt 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 struct tm *t; 60 char tbuf[64]; 61 int errors = 0; 62 int i; 63 64 if (counting) 65 ++count; 66 errno = 0; 67 fprintf(db_file, "lease %s {\n", piaddr(lease->ip_addr)); 68 if (errno) 69 ++errors; 70 71 t = gmtime(&lease->starts); 72 snprintf(tbuf, sizeof(tbuf), "%d %d/%02d/%02d %02d:%02d:%02d;", 73 t->tm_wday, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 74 t->tm_hour, t->tm_min, t->tm_sec); 75 76 errno = 0; 77 fprintf(db_file, "\tstarts %s\n", tbuf); 78 if (errno) 79 ++errors; 80 81 t = gmtime(&lease->ends); 82 snprintf(tbuf, sizeof(tbuf), "%d %d/%02d/%02d %02d:%02d:%02d;", 83 t->tm_wday, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 84 t->tm_hour, t->tm_min, t->tm_sec); 85 86 errno = 0; 87 fprintf(db_file, "\tends %s", tbuf); 88 if (errno) 89 ++errors; 90 91 if (lease->hardware_addr.hlen) { 92 errno = 0; 93 fprintf(db_file, "\n\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)); 98 if (errno) 99 ++errors; 100 } 101 102 if (lease->uid_len) { 103 int j; 104 105 errno = 0; 106 fprintf(db_file, "\n\tuid %2.2x", lease->uid[0]); 107 if (errno) 108 ++errors; 109 110 for (j = 1; j < lease->uid_len; j++) { 111 errno = 0; 112 fprintf(db_file, ":%2.2x", lease->uid[j]); 113 if (errno) 114 ++errors; 115 } 116 putc(';', db_file); 117 } 118 119 if (lease->flags & BOOTP_LEASE) { 120 errno = 0; 121 fprintf(db_file, "\n\tdynamic-bootp;"); 122 if (errno) 123 ++errors; 124 } 125 126 if (lease->flags & ABANDONED_LEASE) { 127 errno = 0; 128 fprintf(db_file, "\n\tabandoned;"); 129 if (errno) 130 ++errors; 131 } 132 133 if (lease->client_hostname) { 134 for (i = 0; lease->client_hostname[i]; i++) 135 if (lease->client_hostname[i] < 33 || 136 lease->client_hostname[i] > 126) 137 goto bad_client_hostname; 138 errno = 0; 139 fprintf(db_file, "\n\tclient-hostname \"%s\";", 140 lease->client_hostname); 141 if (errno) 142 ++errors; 143 } 144 145 bad_client_hostname: 146 if (lease->hostname) { 147 for (i = 0; lease->hostname[i]; i++) 148 if (lease->hostname[i] < 33 || 149 lease->hostname[i] > 126) 150 goto bad_hostname; 151 errno = 0; 152 fprintf(db_file, "\n\thostname \"%s\";", 153 lease->hostname); 154 if (errno) 155 ++errors; 156 } 157 158 bad_hostname: 159 errno = 0; 160 fputs("\n}\n", db_file); 161 if (errno) 162 ++errors; 163 164 if (errors) 165 note("write_lease: unable to write lease %s", 166 piaddr(lease->ip_addr)); 167 168 return (!errors); 169 } 170 171 /* 172 * Commit any leases that have been written out... 173 */ 174 int 175 commit_leases(void) 176 { 177 /* 178 * Commit any outstanding writes to the lease database file. We need to 179 * do this even if we're rewriting the file below, just in case the 180 * rewrite fails. 181 */ 182 if (fflush(db_file) == EOF) { 183 note("commit_leases: unable to commit: %m"); 184 return (0); 185 } 186 187 if (fsync(fileno(db_file)) == -1) { 188 note("commit_leases: unable to commit: %m"); 189 return (0); 190 } 191 192 /* 193 * If we've written more than a thousand leases or if we haven't 194 * rewritten the lease database in over an hour, rewrite it now. 195 */ 196 if (count > 1000 || (count && cur_time - write_time > 3600)) { 197 count = 0; 198 write_time = cur_time; 199 new_lease_file(); 200 } 201 202 return (1); 203 } 204 205 void 206 db_startup(void) 207 { 208 int db_fd; 209 210 /* open lease file. once we dropped privs it has to stay open */ 211 db_fd = open(path_dhcpd_db, O_WRONLY|O_CREAT, 0664); 212 if (db_fd == -1) 213 error("Can't create new lease file: %m"); 214 if ((db_file = fdopen(db_fd, "w")) == NULL) 215 error("Can't fdopen new lease file!"); 216 217 /* Read in the existing lease file... */ 218 read_leases(); 219 time(&write_time); 220 221 new_lease_file(); 222 } 223 224 void 225 new_lease_file(void) 226 { 227 fflush(db_file); 228 rewind(db_file); 229 230 /* 231 * Write an introduction so people don't complain about time being off. 232 */ 233 fprintf(db_file, "# All times in this file are in UTC (GMT), " 234 "not your local timezone.\n"); 235 fprintf(db_file, "# The format of this file is documented in " 236 "the dhcpd.leases(5) manual page.\n\n"); 237 238 /* Write out all the leases that we know of... */ 239 counting = 0; 240 write_leases(); 241 242 fflush(db_file); 243 ftruncate(fileno(db_file), ftello(db_file)); 244 fsync(fileno(db_file)); 245 246 counting = 1; 247 } 248