| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "kadm5_locl.h" |
| 35 |
#include "heim_threads.h" |
| 36 |
|
| 37 |
RCSID("$Id$"); |
| 38 |
|
| 39 |
/* |
| 40 |
* A log consists of a sequence of records of this form: |
| 41 |
* |
| 42 |
* version number 4 bytes -\ |
| 43 |
* time in seconds 4 bytes +> preamble --+> header |
| 44 |
* operation (enum kadm_ops) 4 bytes -/ / |
| 45 |
* n, length of payload 4 bytes --------------+ |
| 46 |
* PAYLOAD DATA... n bytes |
| 47 |
* n, length of payload 4 bytes ----------------+> trailer |
| 48 |
* version number 4 bytes ->postamble ---/ |
| 49 |
* |
| 50 |
* I.e., records have a header and a trailer so that knowing the offset |
| 51 |
* of an record's start or end one can traverse the log forwards and |
| 52 |
* backwards. |
| 53 |
* |
| 54 |
* The log always starts with a nop record (uber record) that contains the |
| 55 |
* offset (8 bytes) of the first unconfirmed record (typically EOF), and the |
| 56 |
* version number and timestamp of the preceding last confirmed record: |
| 57 |
* |
| 58 |
* offset of next new record 8 bytes |
| 59 |
* last record time 4 bytes |
| 60 |
* last record version number 4 bytes |
| 61 |
* |
| 62 |
* When an iprop slave receives a complete database, it saves that version as |
| 63 |
* the last confirmed version, without writing any other records to the log. We |
| 64 |
* use that version as the basis for further updates. |
| 65 |
* |
| 66 |
* kadm5 write operations are done in this order: |
| 67 |
* |
| 68 |
* - replay unconfirmed log records |
| 69 |
* - write (append) and fsync() the log record for the kadm5 update |
| 70 |
* - update the HDB (which includes fsync() or moral equivalent) |
| 71 |
* - update the log uber record to mark the log record written as |
| 72 |
* confirmed (not fsync()ed) |
| 73 |
* |
| 74 |
* This makes it possible and safe to seek to the logical end of the log |
| 75 |
* (that is, the end of the last confirmed record) without traversing |
| 76 |
* the whole log forward from offset zero. Unconfirmed records (which |
| 77 |
* -currently- should never be more than one) can then be found (and |
| 78 |
* rolled forward) by traversing forward from the logical end of the |
| 79 |
* log. The trailers make it possible to traverse the log backwards |
| 80 |
* from the logical end. |
| 81 |
* |
| 82 |
* This also makes the log + the HDB a two-phase commit with |
| 83 |
* roll-forward system. |
| 84 |
* |
| 85 |
* HDB entry exists and HDB entry does not exist errors occurring during |
| 86 |
* replay of unconfirmed records are ignored. This is because the |
| 87 |
* corresponding HDB update might have completed. But also because a |
| 88 |
* change to add aliases to a principal can fail because we don't check |
| 89 |
* for alias conflicts before going ahead with the write operation. |
| 90 |
* |
| 91 |
* Non-sensical and incomplete log records found during roll-forward are |
| 92 |
* truncated. A log record is non-sensical if its header and trailer |
| 93 |
* don't match. |
| 94 |
* |
| 95 |
* Recovery (by rolling forward) occurs at the next read or write by a |
| 96 |
* kadm5 API reader (e.g., kadmin), but not by an hdb API reader (e.g., |
| 97 |
* the KDC). This means that, e.g., a principal rename could fail in |
| 98 |
* between the store and the delete, and recovery might not take place |
| 99 |
* until the next write operation. |
| 100 |
* |
| 101 |
* The log record payload format for create is: |
| 102 |
* |
| 103 |
* DER-encoded HDB_entry n bytes |
| 104 |
* |
| 105 |
* The log record payload format for update is: |
| 106 |
* |
| 107 |
* mask 4 bytes |
| 108 |
* DER-encoded HDB_entry n-4 bytes |
| 109 |
* |
| 110 |
* The log record payload format for delete is: |
| 111 |
* |
| 112 |
* krb5_store_principal n bytes |
| 113 |
* |
| 114 |
* The log record payload format for rename is: |
| 115 |
* |
| 116 |
* krb5_store_principal m bytes (old principal name) |
| 117 |
* DER-encoded HDB_entry n-m bytes (new record) |
| 118 |
* |
| 119 |
* The log record payload format for nop varies: |
| 120 |
* |
| 121 |
* - The zeroth record in new logs is a nop with a 16 byte payload: |
| 122 |
* |
| 123 |
* offset of end of last confirmed record 8 bytes |
| 124 |
* timestamp of last confirmed record 4 bytes |
| 125 |
* version number of last confirmed record 4 bytes |
| 126 |
* |
| 127 |
* - New non-zeroth nop records: |
| 128 |
* |
| 129 |
* nop type 4 bytes |
| 130 |
* |
| 131 |
* - Old nop records: |
| 132 |
* |
| 133 |
* version number 4 bytes |
| 134 |
* timestamp 4 bytes |
| 135 |
* |
| 136 |
* Upon initialization, the log's uber record will have version 1, and |
| 137 |
* will be followed by a nop record with version 2. The version numbers |
| 138 |
* of additional records will be monotonically increasing. |
| 139 |
* |
| 140 |
* Truncation (kadm5_log_truncate()) takes some N > 0 records from the |
| 141 |
* tail of the log and writes them to the beginning of the log after an |
| 142 |
* uber record whose version will then be one less than the first of |
| 143 |
* those records. |
| 144 |
* |
| 145 |
* On masters the log should never have more than one unconfirmed |
| 146 |
* record, but slaves append all of a master's "diffs" and then call |
| 147 |
* kadm5_log_recover() to recover. |
| 148 |
*/ |
| 149 |
|
| 150 |
/* |
| 151 |
* HDB and log lock order on the master: |
| 152 |
* |
| 153 |
* 1) open and lock the HDB |
| 154 |
* 2) open and lock the log |
| 155 |
* 3) do something |
| 156 |
* 4) unlock and close the log |
| 157 |
* 5) repeat (2)..(4) if desired |
| 158 |
* 6) unlock and close the HDB |
| 159 |
* |
| 160 |
* The kadmin -l lock command can be used to hold the HDB open and |
| 161 |
* locked for multiple operations. |
| 162 |
* |
| 163 |
* HDB and log lock order on the slave: |
| 164 |
* |
| 165 |
* 1) open and lock the log |
| 166 |
* 2) open and lock the HDB |
| 167 |
* 3) replay entries |
| 168 |
* 4) unlock and close the HDB |
| 169 |
* 5) repeat (2)..(4) until signaled |
| 170 |
* 6) unlock and close the HDB |
| 171 |
* |
| 172 |
* The slave doesn't want to allow other local writers, after all, thus |
| 173 |
* the order is reversed. This means that using "kadmin -l" on a slave |
| 174 |
* will deadlock with ipropd-slave -- don't do that. |
| 175 |
*/ |
| 176 |
|
| 177 |
#define LOG_HEADER_SZ ((off_t)(sizeof(uint32_t) * 4)) |
| 178 |
#define LOG_TRAILER_SZ ((off_t)(sizeof(uint32_t) * 2)) |
| 179 |
#define LOG_WRAPPER_SZ ((off_t)(LOG_HEADER_SZ + LOG_TRAILER_SZ)) |
| 180 |
#define LOG_UBER_LEN ((off_t)(sizeof(uint64_t) + sizeof(uint32_t) * 2)) |
| 181 |
#define LOG_UBER_SZ ((off_t)(LOG_WRAPPER_SZ + LOG_UBER_LEN)) |
| 182 |
|
| 183 |
#define LOG_NOPEEK 0 |
| 184 |
#define LOG_DOPEEK 1 |
| 185 |
|
| 186 |
/* |
| 187 |
* Read the header of the record starting at the current offset into sp. |
| 188 |
* |
| 189 |
* Preserves sp's offset on success if `peek', else skips the header. |
| 190 |
* |
| 191 |
* Preserves sp's offset on failure where possible. |
| 192 |
*/ |
| 193 |
static kadm5_ret_t |
| 194 |
get_header(krb5_storage *sp, int peek, uint32_t *verp, uint32_t *tstampp, |
| 195 |
enum kadm_ops *opp, uint32_t *lenp) |
| 196 |
{ |
| 197 |
krb5_error_code ret; |
| 198 |
uint32_t tstamp, op, len; |
| 199 |
off_t off, new_off; |
| 200 |
|
| 201 |
if (tstampp == NULL) |
| 202 |
tstampp = &tstamp; |
| 203 |
if (lenp == NULL) |
| 204 |
lenp = &len; |
| 205 |
|
| 206 |
*verp = 0; |
| 207 |
*tstampp = 0; |
| 208 |
if (opp != NULL) |
| 209 |
*opp = kadm_nop; |
| 210 |
*lenp = 0; |
| 211 |
|
| 212 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 213 |
if (off < 0) |
| 214 |
return errno; |
| 215 |
ret = krb5_ret_uint32(sp, verp); |
| 216 |
if (ret == HEIM_ERR_EOF) { |
| 217 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 218 |
return HEIM_ERR_EOF; |
| 219 |
} |
| 220 |
if (ret) |
| 221 |
goto log_corrupt; |
| 222 |
ret = krb5_ret_uint32(sp, tstampp); |
| 223 |
if (ret) |
| 224 |
goto log_corrupt; |
| 225 |
|
| 226 |
/* Note: sizeof(*opp) might not == sizeof(op) */ |
| 227 |
ret = krb5_ret_uint32(sp, &op); |
| 228 |
if (ret) |
| 229 |
goto log_corrupt; |
| 230 |
if (opp != NULL) |
| 231 |
*opp = op; |
| 232 |
|
| 233 |
ret = krb5_ret_uint32(sp, lenp); |
| 234 |
if (ret) |
| 235 |
goto log_corrupt; |
| 236 |
|
| 237 |
/* Restore offset if requested */ |
| 238 |
if (peek == LOG_DOPEEK) { |
| 239 |
new_off = krb5_storage_seek(sp, off, SEEK_SET); |
| 240 |
if (new_off == -1) |
| 241 |
return errno; |
| 242 |
if (new_off != off) |
| 243 |
return EIO; |
| 244 |
} |
| 245 |
|
| 246 |
return 0; |
| 247 |
|
| 248 |
log_corrupt: |
| 249 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 250 |
return KADM5_LOG_CORRUPT; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* Seek to the start of the preceding record's header and returns its |
| 255 |
* offset. If sp is at offset zero this sets *verp = 0 and returns 0. |
| 256 |
* |
| 257 |
* Does not verify the header of the previous entry. |
| 258 |
* |
| 259 |
* On error returns -1, setting errno (possibly to a kadm5_ret_t or |
| 260 |
* krb5_error_code value) and preserves sp's offset where possible. |
| 261 |
*/ |
| 262 |
static off_t |
| 263 |
seek_prev(krb5_storage *sp, uint32_t *verp, uint32_t *lenp) |
| 264 |
{ |
| 265 |
krb5_error_code ret; |
| 266 |
uint32_t len, ver; |
| 267 |
off_t off_len; |
| 268 |
off_t off, new_off; |
| 269 |
|
| 270 |
if (lenp == NULL) |
| 271 |
lenp = &len; |
| 272 |
if (verp == NULL) |
| 273 |
verp = &ver; |
| 274 |
|
| 275 |
*verp = 0; |
| 276 |
*lenp = 0; |
| 277 |
|
| 278 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 279 |
if (off < 0) |
| 280 |
return off; |
| 281 |
if (off == 0) |
| 282 |
return 0; |
| 283 |
|
| 284 |
/* Check that `off' allows for the record's header and trailer */ |
| 285 |
if (off < LOG_WRAPPER_SZ) |
| 286 |
goto log_corrupt; |
| 287 |
|
| 288 |
/* Get the previous entry's length and version from its trailer */ |
| 289 |
new_off = krb5_storage_seek(sp, -8, SEEK_CUR); |
| 290 |
if (new_off == -1) |
| 291 |
return -1; |
| 292 |
if (new_off != off - 8) { |
| 293 |
errno = EIO; |
| 294 |
return -1; |
| 295 |
} |
| 296 |
ret = krb5_ret_uint32(sp, lenp); |
| 297 |
if (ret) |
| 298 |
goto log_corrupt; |
| 299 |
|
| 300 |
/* Check for overflow/sign extension */ |
| 301 |
off_len = (off_t)*lenp; |
| 302 |
if (off_len < 0 || *lenp != (uint32_t)off_len) |
| 303 |
goto log_corrupt; |
| 304 |
|
| 305 |
ret = krb5_ret_uint32(sp, verp); |
| 306 |
if (ret) |
| 307 |
goto log_corrupt; |
| 308 |
|
| 309 |
/* Check that `off' allows for the record */ |
| 310 |
if (off < LOG_WRAPPER_SZ + off_len) |
| 311 |
goto log_corrupt; |
| 312 |
|
| 313 |
/* Seek backwards to the entry's start */ |
| 314 |
new_off = krb5_storage_seek(sp, -(LOG_WRAPPER_SZ + off_len), SEEK_CUR); |
| 315 |
if (new_off == -1) |
| 316 |
return -1; |
| 317 |
if (new_off != off - (LOG_WRAPPER_SZ + off_len)) { |
| 318 |
errno = EIO; |
| 319 |
return -1; |
| 320 |
} |
| 321 |
return new_off; |
| 322 |
|
| 323 |
log_corrupt: |
| 324 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 325 |
errno = KADM5_LOG_CORRUPT; |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* Seek to the start of the next entry's header. |
| 331 |
* |
| 332 |
* On error returns -1 and preserves sp's offset. |
| 333 |
*/ |
| 334 |
static off_t |
| 335 |
seek_next(krb5_storage *sp) |
| 336 |
{ |
| 337 |
krb5_error_code ret; |
| 338 |
uint32_t ver, ver2, len, len2; |
| 339 |
enum kadm_ops op; |
| 340 |
uint32_t tstamp; |
| 341 |
off_t off, off_len, new_off; |
| 342 |
|
| 343 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 344 |
if (off < 0) |
| 345 |
return off; |
| 346 |
|
| 347 |
errno = get_header(sp, LOG_NOPEEK, &ver, &tstamp, &op, &len); |
| 348 |
if (errno) |
| 349 |
return -1; |
| 350 |
|
| 351 |
/* Check for overflow */ |
| 352 |
off_len = len; |
| 353 |
if (off_len < 0) |
| 354 |
goto log_corrupt; |
| 355 |
|
| 356 |
new_off = krb5_storage_seek(sp, off_len, SEEK_CUR); |
| 357 |
if (new_off == -1) { |
| 358 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 359 |
return -1; |
| 360 |
} |
| 361 |
if (new_off != off + LOG_HEADER_SZ + off_len) |
| 362 |
goto log_corrupt; |
| 363 |
ret = krb5_ret_uint32(sp, &len2); |
| 364 |
if (ret || len2 != len) |
| 365 |
goto log_corrupt; |
| 366 |
ret = krb5_ret_uint32(sp, &ver2); |
| 367 |
if (ret || ver2 != ver) |
| 368 |
goto log_corrupt; |
| 369 |
new_off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 370 |
if (new_off == -1) { |
| 371 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 372 |
return -1; |
| 373 |
} |
| 374 |
if (new_off != off + off_len + LOG_WRAPPER_SZ) |
| 375 |
goto log_corrupt; |
| 376 |
|
| 377 |
return off + off_len + LOG_WRAPPER_SZ; |
| 378 |
|
| 379 |
log_corrupt: |
| 380 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 381 |
errno = KADM5_LOG_CORRUPT; |
| 382 |
return -1; |
| 383 |
} |
| 384 |
|
| 385 |
/* |
| 386 |
* Get the version of the entry ending at the current offset into sp. |
| 387 |
* If it is the uber record, return its nominal version instead. |
| 388 |
* |
| 389 |
* Returns HEIM_ERR_EOF if sp is at offset zero. |
| 390 |
* |
| 391 |
* Preserves sp's offset. |
| 392 |
*/ |
| 393 |
static kadm5_ret_t |
| 394 |
get_version_prev(krb5_storage *sp, uint32_t *verp, uint32_t *tstampp) |
| 395 |
{ |
| 396 |
krb5_error_code ret; |
| 397 |
uint32_t ver, ver2, len, len2; |
| 398 |
off_t off, prev_off, new_off; |
| 399 |
|
| 400 |
*verp = 0; |
| 401 |
if (tstampp != NULL) |
| 402 |
*tstampp = 0; |
| 403 |
|
| 404 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 405 |
if (off < 0) |
| 406 |
return errno; |
| 407 |
if (off == 0) |
| 408 |
return HEIM_ERR_EOF; |
| 409 |
|
| 410 |
/* Read the trailer and seek back */ |
| 411 |
prev_off = seek_prev(sp, &ver, &len); |
| 412 |
if (prev_off == -1) |
| 413 |
return errno; |
| 414 |
|
| 415 |
/* Uber record? Return nominal version. */ |
| 416 |
if (prev_off == 0 && len == LOG_UBER_LEN && ver == 0) { |
| 417 |
/* Skip 8 byte offset and 4 byte time */ |
| 418 |
if (krb5_storage_seek(sp, LOG_HEADER_SZ + 12, SEEK_SET) |
| 419 |
!= LOG_HEADER_SZ + 12) |
| 420 |
return errno; |
| 421 |
ret = krb5_ret_uint32(sp, verp); |
| 422 |
if (krb5_storage_seek(sp, 0, SEEK_SET) != 0) |
| 423 |
return errno; |
| 424 |
if (ret != 0) |
| 425 |
return ret; |
| 426 |
} else { |
| 427 |
*verp = ver; |
| 428 |
} |
| 429 |
|
| 430 |
/* Verify that the trailer matches header */ |
| 431 |
ret = get_header(sp, LOG_NOPEEK, &ver2, tstampp, NULL, &len2); |
| 432 |
if (ret || ver != ver2 || len != len2) |
| 433 |
goto log_corrupt; |
| 434 |
|
| 435 |
/* Preserve offset */ |
| 436 |
new_off = krb5_storage_seek(sp, off, SEEK_SET); |
| 437 |
if (new_off == -1) |
| 438 |
return errno; |
| 439 |
if (new_off != off) { |
| 440 |
errno = EIO; |
| 441 |
return errno; |
| 442 |
} |
| 443 |
return 0; |
| 444 |
|
| 445 |
log_corrupt: |
| 446 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 447 |
return KADM5_LOG_CORRUPT; |
| 448 |
} |
| 449 |
|
| 450 |
static size_t |
| 451 |
get_max_log_size(krb5_context context) |
| 452 |
{ |
| 453 |
off_t n; |
| 454 |
|
| 455 |
/* Use database-label-specific lookup? No, ETOOHARD. */ |
| 456 |
/* Default to 50MB max log size */ |
| 457 |
n = krb5_config_get_int_default(context, NULL, 52428800, |
| 458 |
"kdc", |
| 459 |
"log-max-size", |
| 460 |
NULL); |
| 461 |
if (n >= 4 * (LOG_UBER_LEN + LOG_WRAPPER_SZ) && n == (size_t)n) |
| 462 |
return (size_t)n; |
| 463 |
return 0; |
| 464 |
} |
| 465 |
|
| 466 |
static kadm5_ret_t truncate_if_needed(kadm5_server_context *); |
| 467 |
|
| 468 |
/* |
| 469 |
* Get the version and timestamp metadata of either the first, or last |
| 470 |
* confirmed entry in the log. |
| 471 |
* |
| 472 |
* If `which' is LOG_VERSION_UBER, then this gets the version number of the uber |
| 473 |
* uber record which must be 0, or else we need to upgrade the log. |
| 474 |
* |
| 475 |
* If `which' is LOG_VERSION_FIRST, then this gets the metadata for the |
| 476 |
* logically first entry past the uberblock, or returns HEIM_ERR_EOF if |
| 477 |
* only the uber record is present. |
| 478 |
* |
| 479 |
* If `which' is LOG_VERSION_LAST, then this gets metadata for the last |
| 480 |
* confirmed entry's version and timestamp. If only the uber record is present, |
| 481 |
* then the version will be its "nominal" version, which may differ from its |
| 482 |
* actual version (0). |
| 483 |
* |
| 484 |
* The `fd''s offset will be set to the start of the header of the entry |
| 485 |
* identified by `which'. |
| 486 |
*/ |
| 487 |
kadm5_ret_t |
| 488 |
kadm5_log_get_version_fd(kadm5_server_context *server_context, int fd, |
| 489 |
int which, uint32_t *ver, uint32_t *tstamp) |
| 490 |
{ |
| 491 |
kadm5_ret_t ret = 0; |
| 492 |
krb5_storage *sp; |
| 493 |
enum kadm_ops op = kadm_get; |
| 494 |
uint32_t len = 0; |
| 495 |
uint32_t tmp; |
| 496 |
|
| 497 |
if (fd == -1) |
| 498 |
return 0; /* /dev/null */ |
| 499 |
|
| 500 |
if (tstamp == NULL) |
| 501 |
tstamp = &tmp; |
| 502 |
|
| 503 |
*ver = 0; |
| 504 |
*tstamp = 0; |
| 505 |
|
| 506 |
sp = krb5_storage_from_fd(fd); |
| 507 |
if (sp == NULL) |
| 508 |
return errno ? errno : ENOMEM; |
| 509 |
|
| 510 |
switch (which) { |
| 511 |
case LOG_VERSION_LAST: |
| 512 |
ret = kadm5_log_goto_end(server_context, sp); |
| 513 |
if (ret == 0) |
| 514 |
ret = get_version_prev(sp, ver, tstamp); |
| 515 |
break; |
| 516 |
case LOG_VERSION_FIRST: |
| 517 |
ret = kadm5_log_goto_first(server_context, sp); |
| 518 |
if (ret == 0) |
| 519 |
ret = get_header(sp, LOG_DOPEEK, ver, tstamp, NULL, NULL); |
| 520 |
break; |
| 521 |
case LOG_VERSION_UBER: |
| 522 |
if (krb5_storage_seek(sp, 0, SEEK_SET) == 0) |
| 523 |
ret = get_header(sp, LOG_DOPEEK, ver, tstamp, &op, &len); |
| 524 |
else |
| 525 |
ret = errno; |
| 526 |
if (ret == 0 && (op != kadm_nop || len != LOG_UBER_LEN || *ver != 0)) |
| 527 |
ret = KADM5_LOG_NEEDS_UPGRADE; |
| 528 |
break; |
| 529 |
default: |
| 530 |
ret = ENOTSUP; |
| 531 |
break; |
| 532 |
} |
| 533 |
|
| 534 |
krb5_storage_free(sp); |
| 535 |
return ret; |
| 536 |
} |
| 537 |
|
| 538 |
/* Get the version of the last confirmed entry in the log */ |
| 539 |
kadm5_ret_t |
| 540 |
kadm5_log_get_version(kadm5_server_context *server_context, uint32_t *ver) |
| 541 |
{ |
| 542 |
return kadm5_log_get_version_fd(server_context, |
| 543 |
server_context->log_context.log_fd, |
| 544 |
LOG_VERSION_LAST, ver, NULL); |
| 545 |
} |
| 546 |
|
| 547 |
/* Sets the version in the context, but NOT in the log */ |
| 548 |
kadm5_ret_t |
| 549 |
kadm5_log_set_version(kadm5_server_context *context, uint32_t vno) |
| 550 |
{ |
| 551 |
kadm5_log_context *log_context = &context->log_context; |
| 552 |
|
| 553 |
log_context->version = vno; |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Open the log and setup server_context->log_context |
| 559 |
*/ |
| 560 |
static kadm5_ret_t |
| 561 |
log_open(kadm5_server_context *server_context, int lock_mode) |
| 562 |
{ |
| 563 |
int fd = -1; |
| 564 |
int lock_it = 0; |
| 565 |
int lock_nb = 0; |
| 566 |
int oflags = O_RDWR; |
| 567 |
kadm5_ret_t ret; |
| 568 |
kadm5_log_context *log_context = &server_context->log_context; |
| 569 |
|
| 570 |
if (lock_mode & LOCK_NB) { |
| 571 |
lock_mode &= ~LOCK_NB; |
| 572 |
lock_nb = LOCK_NB; |
| 573 |
} |
| 574 |
|
| 575 |
if (lock_mode == log_context->lock_mode && log_context->log_fd != -1) |
| 576 |
return 0; |
| 577 |
|
| 578 |
if (strcmp(log_context->log_file, "/dev/null") == 0) { |
| 579 |
/* log_context->log_fd should be -1 here */ |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
if (log_context->log_fd != -1) { |
| 584 |
/* Lock or change lock */ |
| 585 |
fd = log_context->log_fd; |
| 586 |
if (lseek(fd, 0, SEEK_SET) == -1) |
| 587 |
return errno; |
| 588 |
lock_it = (lock_mode != log_context->lock_mode); |
| 589 |
} else { |
| 590 |
/* Open and lock */ |
| 591 |
if (lock_mode != LOCK_UN) |
| 592 |
oflags |= O_CREAT; |
| 593 |
fd = open(log_context->log_file, oflags, 0600); |
| 594 |
if (fd < 0) { |
| 595 |
ret = errno; |
| 596 |
krb5_set_error_message(server_context->context, ret, |
| 597 |
"log_open: open %s", log_context->log_file); |
| 598 |
return ret; |
| 599 |
} |
| 600 |
lock_it = (lock_mode != LOCK_UN); |
| 601 |
} |
| 602 |
if (lock_it && flock(fd, lock_mode | lock_nb) < 0) { |
| 603 |
ret = errno; |
| 604 |
krb5_set_error_message(server_context->context, ret, |
| 605 |
"log_open: flock %s", log_context->log_file); |
| 606 |
if (fd != log_context->log_fd) |
| 607 |
(void) close(fd); |
| 608 |
return ret; |
| 609 |
} |
| 610 |
|
| 611 |
log_context->log_fd = fd; |
| 612 |
log_context->lock_mode = lock_mode; |
| 613 |
log_context->read_only = (lock_mode != LOCK_EX); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
/* |
| 619 |
* Open the log and setup server_context->log_context |
| 620 |
*/ |
| 621 |
static kadm5_ret_t |
| 622 |
log_init(kadm5_server_context *server_context, int lock_mode) |
| 623 |
{ |
| 624 |
int fd; |
| 625 |
struct stat st; |
| 626 |
uint32_t vno; |
| 627 |
size_t maxbytes = get_max_log_size(server_context->context); |
| 628 |
kadm5_ret_t ret; |
| 629 |
kadm5_log_context *log_context = &server_context->log_context; |
| 630 |
|
| 631 |
if (strcmp(log_context->log_file, "/dev/null") == 0) { |
| 632 |
/* log_context->log_fd should be -1 here */ |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
ret = log_open(server_context, lock_mode); |
| 637 |
if (ret) |
| 638 |
return ret; |
| 639 |
|
| 640 |
fd = log_context->log_fd; |
| 641 |
if (!log_context->read_only) { |
| 642 |
if (fstat(fd, &st) == -1) |
| 643 |
ret = errno; |
| 644 |
if (ret == 0 && st.st_size == 0) { |
| 645 |
/* Write first entry */ |
| 646 |
log_context->version = 0; |
| 647 |
ret = kadm5_log_nop(server_context, kadm_nop_plain); |
| 648 |
if (ret == 0) |
| 649 |
return 0; /* no need to truncate_if_needed(): it's not */ |
| 650 |
} |
| 651 |
if (ret == 0) { |
| 652 |
ret = kadm5_log_get_version_fd(server_context, fd, |
| 653 |
LOG_VERSION_UBER, &vno, NULL); |
| 654 |
|
| 655 |
/* Upgrade the log if it was an old-style log */ |
| 656 |
if (ret == KADM5_LOG_NEEDS_UPGRADE) |
| 657 |
ret = kadm5_log_truncate(server_context, 0, maxbytes / 4); |
| 658 |
} |
| 659 |
if (ret == 0) |
| 660 |
ret = kadm5_log_recover(server_context, kadm_recover_replay); |
| 661 |
} |
| 662 |
|
| 663 |
if (ret == 0) { |
| 664 |
ret = kadm5_log_get_version_fd(server_context, fd, LOG_VERSION_LAST, |
| 665 |
&log_context->version, NULL); |
| 666 |
if (ret == HEIM_ERR_EOF) |
| 667 |
ret = 0; |
| 668 |
} |
| 669 |
|
| 670 |
if (ret == 0) |
| 671 |
ret = truncate_if_needed(server_context); |
| 672 |
|
| 673 |
if (ret != 0) |
| 674 |
(void) kadm5_log_end(server_context); |
| 675 |
return ret; |
| 676 |
} |
| 677 |
|
| 678 |
/* Open the log with an exclusive lock */ |
| 679 |
kadm5_ret_t |
| 680 |
kadm5_log_init(kadm5_server_context *server_context) |
| 681 |
{ |
| 682 |
return log_init(server_context, LOCK_EX); |
| 683 |
} |
| 684 |
|
| 685 |
/* Open the log with an exclusive non-blocking lock */ |
| 686 |
kadm5_ret_t |
| 687 |
kadm5_log_init_nb(kadm5_server_context *server_context) |
| 688 |
{ |
| 689 |
return log_init(server_context, LOCK_EX | LOCK_NB); |
| 690 |
} |
| 691 |
|
| 692 |
/* Open the log with no locks */ |
| 693 |
kadm5_ret_t |
| 694 |
kadm5_log_init_nolock(kadm5_server_context *server_context) |
| 695 |
{ |
| 696 |
return log_init(server_context, LOCK_UN); |
| 697 |
} |
| 698 |
|
| 699 |
/* Open the log with a shared lock */ |
| 700 |
kadm5_ret_t |
| 701 |
kadm5_log_init_sharedlock(kadm5_server_context *server_context, int lock_flags) |
| 702 |
{ |
| 703 |
return log_init(server_context, LOCK_SH | lock_flags); |
| 704 |
} |
| 705 |
|
| 706 |
/* |
| 707 |
* Reinitialize the log and open it |
| 708 |
*/ |
| 709 |
kadm5_ret_t |
| 710 |
kadm5_log_reinit(kadm5_server_context *server_context, uint32_t vno) |
| 711 |
{ |
| 712 |
int ret; |
| 713 |
kadm5_log_context *log_context = &server_context->log_context; |
| 714 |
|
| 715 |
ret = log_open(server_context, LOCK_EX); |
| 716 |
if (ret) |
| 717 |
return ret; |
| 718 |
if (log_context->log_fd != -1) { |
| 719 |
if (ftruncate(log_context->log_fd, 0) < 0) { |
| 720 |
ret = errno; |
| 721 |
return ret; |
| 722 |
} |
| 723 |
if (lseek(log_context->log_fd, 0, SEEK_SET) < 0) { |
| 724 |
ret = errno; |
| 725 |
return ret; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
/* Write uber entry and truncation nop with version `vno` */ |
| 730 |
log_context->version = vno; |
| 731 |
return kadm5_log_nop(server_context, kadm_nop_plain); |
| 732 |
} |
| 733 |
|
| 734 |
/* Close the server_context->log_context. */ |
| 735 |
kadm5_ret_t |
| 736 |
kadm5_log_end(kadm5_server_context *server_context) |
| 737 |
{ |
| 738 |
kadm5_log_context *log_context = &server_context->log_context; |
| 739 |
kadm5_ret_t ret = 0; |
| 740 |
int fd = log_context->log_fd; |
| 741 |
|
| 742 |
if (fd != -1) { |
| 743 |
if (log_context->lock_mode != LOCK_UN) { |
| 744 |
if (flock(fd, LOCK_UN) == -1 && errno == EBADF) |
| 745 |
ret = errno; |
| 746 |
} |
| 747 |
if (ret != EBADF && close(fd) == -1) |
| 748 |
ret = errno; |
| 749 |
} |
| 750 |
log_context->log_fd = -1; |
| 751 |
log_context->lock_mode = LOCK_UN; |
| 752 |
return ret; |
| 753 |
} |
| 754 |
|
| 755 |
/* |
| 756 |
* Write the version, timestamp, and op for a new entry. |
| 757 |
* |
| 758 |
* Note that the sp should be a krb5_storage_emem(), not a file. |
| 759 |
* |
| 760 |
* On success the sp's offset will be where the length of the payload |
| 761 |
* should be written. |
| 762 |
*/ |
| 763 |
static kadm5_ret_t |
| 764 |
kadm5_log_preamble(kadm5_server_context *context, |
| 765 |
krb5_storage *sp, |
| 766 |
enum kadm_ops op, |
| 767 |
uint32_t vno) |
| 768 |
{ |
| 769 |
kadm5_log_context *log_context = &context->log_context; |
| 770 |
time_t now = time(NULL); |
| 771 |
kadm5_ret_t ret; |
| 772 |
|
| 773 |
ret = krb5_store_uint32(sp, vno); |
| 774 |
if (ret) |
| 775 |
return ret; |
| 776 |
ret = krb5_store_uint32(sp, now); |
| 777 |
if (ret) |
| 778 |
return ret; |
| 779 |
log_context->last_time = now; |
| 780 |
|
| 781 |
if (op < kadm_first || op > kadm_last) |
| 782 |
return ERANGE; |
| 783 |
return krb5_store_uint32(sp, op); |
| 784 |
} |
| 785 |
|
| 786 |
/* Writes the version part of the trailer */ |
| 787 |
static kadm5_ret_t |
| 788 |
kadm5_log_postamble(kadm5_log_context *context, |
| 789 |
krb5_storage *sp, |
| 790 |
uint32_t vno) |
| 791 |
{ |
| 792 |
return krb5_store_uint32(sp, vno); |
| 793 |
} |
| 794 |
|
| 795 |
/* |
| 796 |
* Signal the ipropd-master about changes to the log. |
| 797 |
*/ |
| 798 |
/* |
| 799 |
* XXX Get rid of the ifdef by having a sockaddr in log_context in both |
| 800 |
* cases. |
| 801 |
* |
| 802 |
* XXX Better yet, just connect to the master's socket that slaves |
| 803 |
* connect to, and then disconnect. The master should then check the |
| 804 |
* log on every connection accepted. Then we wouldn't need IPC to |
| 805 |
* signal the master. |
| 806 |
*/ |
| 807 |
void |
| 808 |
kadm5_log_signal_master(kadm5_server_context *context) |
| 809 |
{ |
| 810 |
kadm5_log_context *log_context = &context->log_context; |
| 811 |
#ifndef NO_UNIX_SOCKETS |
| 812 |
sendto(log_context->socket_fd, |
| 813 |
(void *)&log_context->version, |
| 814 |
sizeof(log_context->version), |
| 815 |
0, |
| 816 |
(struct sockaddr *)&log_context->socket_name, |
| 817 |
sizeof(log_context->socket_name)); |
| 818 |
#else |
| 819 |
sendto(log_context->socket_fd, |
| 820 |
(void *)&log_context->version, |
| 821 |
sizeof(log_context->version), |
| 822 |
0, |
| 823 |
log_context->socket_info->ai_addr, |
| 824 |
log_context->socket_info->ai_addrlen); |
| 825 |
#endif |
| 826 |
} |
| 827 |
|
| 828 |
/* |
| 829 |
* Write sp's contents (which must be a fully formed record, complete |
| 830 |
* with header, payload, and trailer) to the log and fsync the log. |
| 831 |
* |
| 832 |
* Does not free sp. |
| 833 |
*/ |
| 834 |
|
| 835 |
static kadm5_ret_t |
| 836 |
kadm5_log_flush(kadm5_server_context *context, krb5_storage *sp) |
| 837 |
{ |
| 838 |
kadm5_log_context *log_context = &context->log_context; |
| 839 |
kadm5_ret_t ret; |
| 840 |
krb5_data data; |
| 841 |
size_t len; |
| 842 |
krb5_ssize_t bytes; |
| 843 |
uint32_t new_ver, prev_ver; |
| 844 |
off_t off, end; |
| 845 |
|
| 846 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 847 |
return 0; |
| 848 |
|
| 849 |
if (log_context->read_only) |
| 850 |
return EROFS; |
| 851 |
|
| 852 |
if (krb5_storage_seek(sp, 0, SEEK_SET) == -1) |
| 853 |
return errno; |
| 854 |
|
| 855 |
ret = get_header(sp, LOG_DOPEEK, &new_ver, NULL, NULL, NULL); |
| 856 |
if (ret) |
| 857 |
return ret; |
| 858 |
|
| 859 |
ret = krb5_storage_to_data(sp, &data); |
| 860 |
if (ret) |
| 861 |
return ret; |
| 862 |
|
| 863 |
/* Abandon the emem storage reference */ |
| 864 |
sp = krb5_storage_from_fd(log_context->log_fd); |
| 865 |
if (sp == NULL) { |
| 866 |
krb5_data_free(&data); |
| 867 |
return ENOMEM; |
| 868 |
} |
| 869 |
|
| 870 |
/* Check that we are at the end of the log and fail if not */ |
| 871 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 872 |
if (off == -1) { |
| 873 |
krb5_data_free(&data); |
| 874 |
krb5_storage_free(sp); |
| 875 |
return errno; |
| 876 |
} |
| 877 |
end = krb5_storage_seek(sp, 0, SEEK_END); |
| 878 |
if (end == -1) { |
| 879 |
krb5_data_free(&data); |
| 880 |
krb5_storage_free(sp); |
| 881 |
return errno; |
| 882 |
} |
| 883 |
if (end != off) { |
| 884 |
krb5_data_free(&data); |
| 885 |
krb5_storage_free(sp); |
| 886 |
return KADM5_LOG_CORRUPT; |
| 887 |
} |
| 888 |
|
| 889 |
/* Enforce monotonically incremented versioning of records */ |
| 890 |
if (seek_prev(sp, &prev_ver, NULL) == -1 || |
| 891 |
krb5_storage_seek(sp, end, SEEK_SET) == -1) { |
| 892 |
ret = errno; |
| 893 |
krb5_data_free(&data); |
| 894 |
krb5_storage_free(sp); |
| 895 |
return ret; |
| 896 |
} |
| 897 |
|
| 898 |
if (prev_ver != 0 && prev_ver != log_context->version) |
| 899 |
return EINVAL; /* Internal error, really; just a consistency check */ |
| 900 |
|
| 901 |
if (prev_ver != 0 && new_ver != prev_ver + 1) { |
| 902 |
krb5_warnx(context->context, "refusing to write a log record " |
| 903 |
"with non-monotonic version (new: %u, old: %u)", |
| 904 |
new_ver, prev_ver); |
| 905 |
return KADM5_LOG_CORRUPT; |
| 906 |
} |
| 907 |
|
| 908 |
len = data.length; |
| 909 |
bytes = krb5_storage_write(sp, data.data, len); |
| 910 |
krb5_data_free(&data); |
| 911 |
if (bytes < 0) { |
| 912 |
krb5_storage_free(sp); |
| 913 |
return errno; |
| 914 |
} |
| 915 |
if (bytes != (krb5_ssize_t)len) { |
| 916 |
krb5_storage_free(sp); |
| 917 |
return EIO; |
| 918 |
} |
| 919 |
|
| 920 |
ret = krb5_storage_fsync(sp); |
| 921 |
krb5_storage_free(sp); |
| 922 |
if (ret) |
| 923 |
return ret; |
| 924 |
|
| 925 |
/* Retain the nominal database version when flushing the uber record */ |
| 926 |
if (new_ver != 0) |
| 927 |
log_context->version = new_ver; |
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
/* |
| 932 |
* Add a `create' operation to the log and perform the create against the HDB. |
| 933 |
*/ |
| 934 |
kadm5_ret_t |
| 935 |
kadm5_log_create(kadm5_server_context *context, hdb_entry *entry) |
| 936 |
{ |
| 937 |
krb5_storage *sp; |
| 938 |
kadm5_ret_t ret; |
| 939 |
krb5_data value; |
| 940 |
hdb_entry_ex ent; |
| 941 |
kadm5_log_context *log_context = &context->log_context; |
| 942 |
|
| 943 |
memset(&ent, 0, sizeof(ent)); |
| 944 |
ent.ctx = 0; |
| 945 |
ent.free_entry = 0; |
| 946 |
ent.entry = *entry; |
| 947 |
|
| 948 |
/* |
| 949 |
* If we're not logging then we can't recover-to-perform, so just |
| 950 |
* perform. |
| 951 |
*/ |
| 952 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 953 |
return context->db->hdb_store(context->context, context->db, 0, &ent); |
| 954 |
|
| 955 |
/* |
| 956 |
* Test for any conflicting entries before writing the log. If we commit |
| 957 |
* to the log we'll end-up rolling forward on recovery, but that would be |
| 958 |
* wrong if the initial create is rejected. |
| 959 |
*/ |
| 960 |
ret = context->db->hdb_store(context->context, context->db, |
| 961 |
HDB_F_PRECHECK, &ent); |
| 962 |
if (ret == 0) |
| 963 |
ret = hdb_entry2value(context->context, entry, &value); |
| 964 |
if (ret) |
| 965 |
return ret; |
| 966 |
sp = krb5_storage_emem(); |
| 967 |
if (sp == NULL) |
| 968 |
ret = ENOMEM; |
| 969 |
if (ret == 0) |
| 970 |
ret = kadm5_log_preamble(context, sp, kadm_create, |
| 971 |
log_context->version + 1); |
| 972 |
if (ret == 0) |
| 973 |
ret = krb5_store_uint32(sp, value.length); |
| 974 |
if (ret == 0) { |
| 975 |
if (krb5_storage_write(sp, value.data, value.length) != |
| 976 |
(krb5_ssize_t)value.length) |
| 977 |
ret = errno; |
| 978 |
} |
| 979 |
if (ret == 0) |
| 980 |
ret = krb5_store_uint32(sp, value.length); |
| 981 |
if (ret == 0) |
| 982 |
ret = kadm5_log_postamble(log_context, sp, |
| 983 |
log_context->version + 1); |
| 984 |
if (ret == 0) |
| 985 |
ret = kadm5_log_flush(context, sp); |
| 986 |
krb5_storage_free(sp); |
| 987 |
krb5_data_free(&value); |
| 988 |
if (ret == 0) |
| 989 |
ret = kadm5_log_recover(context, kadm_recover_commit); |
| 990 |
return ret; |
| 991 |
} |
| 992 |
|
| 993 |
/* |
| 994 |
* Read the data of a create log record from `sp' and change the |
| 995 |
* database. |
| 996 |
*/ |
| 997 |
static kadm5_ret_t |
| 998 |
kadm5_log_replay_create(kadm5_server_context *context, |
| 999 |
uint32_t ver, |
| 1000 |
uint32_t len, |
| 1001 |
krb5_storage *sp) |
| 1002 |
{ |
| 1003 |
krb5_error_code ret; |
| 1004 |
krb5_data data; |
| 1005 |
hdb_entry_ex ent; |
| 1006 |
|
| 1007 |
memset(&ent, 0, sizeof(ent)); |
| 1008 |
|
| 1009 |
ret = krb5_data_alloc(&data, len); |
| 1010 |
if (ret) { |
| 1011 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1012 |
return ret; |
| 1013 |
} |
| 1014 |
krb5_storage_read(sp, data.data, len); |
| 1015 |
ret = hdb_value2entry(context->context, &data, &ent.entry); |
| 1016 |
krb5_data_free(&data); |
| 1017 |
if (ret) { |
| 1018 |
krb5_set_error_message(context->context, ret, |
| 1019 |
"Unmarshaling hdb entry in log failed, " |
| 1020 |
"version: %ld", (long)ver); |
| 1021 |
return ret; |
| 1022 |
} |
| 1023 |
ret = context->db->hdb_store(context->context, context->db, 0, &ent); |
| 1024 |
hdb_free_entry(context->context, &ent); |
| 1025 |
return ret; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* Add a `delete' operation to the log. |
| 1030 |
*/ |
| 1031 |
kadm5_ret_t |
| 1032 |
kadm5_log_delete(kadm5_server_context *context, |
| 1033 |
krb5_principal princ) |
| 1034 |
{ |
| 1035 |
kadm5_ret_t ret; |
| 1036 |
kadm5_log_context *log_context = &context->log_context; |
| 1037 |
krb5_storage *sp; |
| 1038 |
uint32_t len = 0; /* So dumb compilers don't warn */ |
| 1039 |
off_t end_off = 0; /* Ditto; this allows de-indentation by two levels */ |
| 1040 |
off_t off; |
| 1041 |
|
| 1042 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 1043 |
return context->db->hdb_remove(context->context, context->db, 0, |
| 1044 |
princ); |
| 1045 |
ret = context->db->hdb_remove(context->context, context->db, |
| 1046 |
HDB_F_PRECHECK, princ); |
| 1047 |
if (ret) |
| 1048 |
return ret; |
| 1049 |
sp = krb5_storage_emem(); |
| 1050 |
if (sp == NULL) |
| 1051 |
ret = ENOMEM; |
| 1052 |
if (ret == 0) |
| 1053 |
ret = kadm5_log_preamble(context, sp, kadm_delete, |
| 1054 |
log_context->version + 1); |
| 1055 |
if (ret) { |
| 1056 |
krb5_storage_free(sp); |
| 1057 |
return ret; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* |
| 1061 |
* Write a 0 length which we overwrite once we know the length of |
| 1062 |
* the principal name payload. |
| 1063 |
*/ |
| 1064 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1065 |
if (off == -1) |
| 1066 |
ret = errno; |
| 1067 |
if (ret == 0) |
| 1068 |
ret = krb5_store_uint32(sp, 0); |
| 1069 |
if (ret == 0) |
| 1070 |
ret = krb5_store_principal(sp, princ); |
| 1071 |
if (ret == 0) { |
| 1072 |
end_off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1073 |
if (end_off == -1) |
| 1074 |
ret = errno; |
| 1075 |
else if (end_off < off) |
| 1076 |
ret = KADM5_LOG_CORRUPT; |
| 1077 |
} |
| 1078 |
if (ret == 0) { |
| 1079 |
/* We wrote sizeof(uint32_t) + payload length bytes */ |
| 1080 |
len = (uint32_t)(end_off - off); |
| 1081 |
if (end_off - off != len || len < sizeof(len)) |
| 1082 |
ret = KADM5_LOG_CORRUPT; |
| 1083 |
else |
| 1084 |
len -= sizeof(len); |
| 1085 |
} |
| 1086 |
if (ret == 0 && krb5_storage_seek(sp, off, SEEK_SET) == -1) |
| 1087 |
ret = errno; |
| 1088 |
if (ret == 0) |
| 1089 |
ret = krb5_store_uint32(sp, len); |
| 1090 |
if (ret == 0 && krb5_storage_seek(sp, end_off, SEEK_SET) == -1) |
| 1091 |
ret = errno; |
| 1092 |
if (ret == 0) |
| 1093 |
ret = krb5_store_uint32(sp, len); |
| 1094 |
if (ret == 0) |
| 1095 |
ret = kadm5_log_postamble(log_context, sp, |
| 1096 |
log_context->version + 1); |
| 1097 |
if (ret == 0) |
| 1098 |
ret = kadm5_log_flush(context, sp); |
| 1099 |
if (ret == 0) |
| 1100 |
ret = kadm5_log_recover(context, kadm_recover_commit); |
| 1101 |
krb5_storage_free(sp); |
| 1102 |
return ret; |
| 1103 |
} |
| 1104 |
|
| 1105 |
/* |
| 1106 |
* Read a `delete' log operation from `sp' and apply it. |
| 1107 |
*/ |
| 1108 |
static kadm5_ret_t |
| 1109 |
kadm5_log_replay_delete(kadm5_server_context *context, |
| 1110 |
uint32_t ver, uint32_t len, krb5_storage *sp) |
| 1111 |
{ |
| 1112 |
krb5_error_code ret; |
| 1113 |
krb5_principal principal; |
| 1114 |
|
| 1115 |
ret = krb5_ret_principal(sp, &principal); |
| 1116 |
if (ret) { |
| 1117 |
krb5_set_error_message(context->context, ret, "Failed to read deleted " |
| 1118 |
"principal from log version: %ld", (long)ver); |
| 1119 |
return ret; |
| 1120 |
} |
| 1121 |
|
| 1122 |
ret = context->db->hdb_remove(context->context, context->db, 0, principal); |
| 1123 |
krb5_free_principal(context->context, principal); |
| 1124 |
return ret; |
| 1125 |
} |
| 1126 |
|
| 1127 |
static kadm5_ret_t kadm5_log_replay_rename(kadm5_server_context *, |
| 1128 |
uint32_t, uint32_t, |
| 1129 |
krb5_storage *); |
| 1130 |
|
| 1131 |
/* |
| 1132 |
* Add a `rename' operation to the log. |
| 1133 |
*/ |
| 1134 |
kadm5_ret_t |
| 1135 |
kadm5_log_rename(kadm5_server_context *context, |
| 1136 |
krb5_principal source, |
| 1137 |
hdb_entry *entry) |
| 1138 |
{ |
| 1139 |
krb5_storage *sp; |
| 1140 |
kadm5_ret_t ret; |
| 1141 |
uint32_t len = 0; /* So dumb compilers don't warn */ |
| 1142 |
off_t end_off = 0; /* Ditto; this allows de-indentation by two levels */ |
| 1143 |
off_t off; |
| 1144 |
krb5_data value; |
| 1145 |
hdb_entry_ex ent; |
| 1146 |
kadm5_log_context *log_context = &context->log_context; |
| 1147 |
|
| 1148 |
memset(&ent, 0, sizeof(ent)); |
| 1149 |
ent.ctx = 0; |
| 1150 |
ent.free_entry = 0; |
| 1151 |
ent.entry = *entry; |
| 1152 |
|
| 1153 |
if (strcmp(log_context->log_file, "/dev/null") == 0) { |
| 1154 |
ret = context->db->hdb_store(context->context, context->db, 0, &ent); |
| 1155 |
if (ret == 0) |
| 1156 |
return context->db->hdb_remove(context->context, context->db, 0, |
| 1157 |
source); |
| 1158 |
return ret; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* |
| 1162 |
* Pre-check that the transaction will succeed. |
| 1163 |
* |
| 1164 |
* Note that rename doesn't work to swap a principal's canonical |
| 1165 |
* name with one of its aliases. To make that work would require |
| 1166 |
* adding an hdb_rename() method for renaming principals (there's an |
| 1167 |
* hdb_rename() method already, but for renaming the HDB), which is |
| 1168 |
* ETOOMUCHWORK for the time being. |
| 1169 |
*/ |
| 1170 |
ret = context->db->hdb_store(context->context, context->db, |
| 1171 |
HDB_F_PRECHECK, &ent); |
| 1172 |
if (ret == 0) |
| 1173 |
ret = context->db->hdb_remove(context->context, context->db, |
| 1174 |
HDB_F_PRECHECK, source); |
| 1175 |
if (ret) |
| 1176 |
return ret; |
| 1177 |
|
| 1178 |
sp = krb5_storage_emem(); |
| 1179 |
krb5_data_zero(&value); |
| 1180 |
if (sp == NULL) |
| 1181 |
ret = ENOMEM; |
| 1182 |
if (ret == 0) |
| 1183 |
ret = kadm5_log_preamble(context, sp, kadm_rename, |
| 1184 |
log_context->version + 1); |
| 1185 |
if (ret == 0) |
| 1186 |
ret = hdb_entry2value(context->context, entry, &value); |
| 1187 |
if (ret) { |
| 1188 |
krb5_data_free(&value); |
| 1189 |
krb5_storage_free(sp); |
| 1190 |
return ret; |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* |
| 1194 |
* Write a zero length which we'll overwrite once we know the length of the |
| 1195 |
* payload. |
| 1196 |
*/ |
| 1197 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1198 |
if (off == -1) |
| 1199 |
ret = errno; |
| 1200 |
if (ret == 0) |
| 1201 |
ret = krb5_store_uint32(sp, 0); |
| 1202 |
if (ret == 0) |
| 1203 |
ret = krb5_store_principal(sp, source); |
| 1204 |
if (ret == 0) { |
| 1205 |
errno = 0; |
| 1206 |
if (krb5_storage_write(sp, value.data, value.length) != |
| 1207 |
(krb5_ssize_t)value.length) |
| 1208 |
ret = errno ? errno : EIO; |
| 1209 |
} |
| 1210 |
if (ret == 0) { |
| 1211 |
end_off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1212 |
if (end_off == -1) |
| 1213 |
ret = errno; |
| 1214 |
else if (end_off < off) |
| 1215 |
ret = KADM5_LOG_CORRUPT; |
| 1216 |
} |
| 1217 |
if (ret == 0) { |
| 1218 |
/* We wrote sizeof(uint32_t) + payload length bytes */ |
| 1219 |
len = (uint32_t)(end_off - off); |
| 1220 |
if (end_off - off != len || len < sizeof(len)) |
| 1221 |
ret = KADM5_LOG_CORRUPT; |
| 1222 |
else |
| 1223 |
len -= sizeof(len); |
| 1224 |
if (ret == 0 && krb5_storage_seek(sp, off, SEEK_SET) == -1) |
| 1225 |
ret = errno; |
| 1226 |
if (ret == 0) |
| 1227 |
ret = krb5_store_uint32(sp, len); |
| 1228 |
if (ret == 0 && krb5_storage_seek(sp, end_off, SEEK_SET) == -1) |
| 1229 |
ret = errno; |
| 1230 |
if (ret == 0) |
| 1231 |
ret = krb5_store_uint32(sp, len); |
| 1232 |
if (ret == 0) |
| 1233 |
ret = kadm5_log_postamble(log_context, sp, |
| 1234 |
log_context->version + 1); |
| 1235 |
if (ret == 0) |
| 1236 |
ret = kadm5_log_flush(context, sp); |
| 1237 |
if (ret == 0) |
| 1238 |
ret = kadm5_log_recover(context, kadm_recover_commit); |
| 1239 |
} |
| 1240 |
krb5_data_free(&value); |
| 1241 |
krb5_storage_free(sp); |
| 1242 |
return ret; |
| 1243 |
} |
| 1244 |
|
| 1245 |
/* |
| 1246 |
* Read a `rename' log operation from `sp' and apply it. |
| 1247 |
*/ |
| 1248 |
|
| 1249 |
static kadm5_ret_t |
| 1250 |
kadm5_log_replay_rename(kadm5_server_context *context, |
| 1251 |
uint32_t ver, |
| 1252 |
uint32_t len, |
| 1253 |
krb5_storage *sp) |
| 1254 |
{ |
| 1255 |
krb5_error_code ret; |
| 1256 |
krb5_principal source; |
| 1257 |
hdb_entry_ex target_ent; |
| 1258 |
krb5_data value; |
| 1259 |
off_t off; |
| 1260 |
size_t princ_len, data_len; |
| 1261 |
|
| 1262 |
memset(&target_ent, 0, sizeof(target_ent)); |
| 1263 |
|
| 1264 |
off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1265 |
ret = krb5_ret_principal(sp, &source); |
| 1266 |
if (ret) { |
| 1267 |
krb5_set_error_message(context->context, ret, "Failed to read renamed " |
| 1268 |
"principal in log, version: %ld", (long)ver); |
| 1269 |
return ret; |
| 1270 |
} |
| 1271 |
princ_len = krb5_storage_seek(sp, 0, SEEK_CUR) - off; |
| 1272 |
data_len = len - princ_len; |
| 1273 |
ret = krb5_data_alloc(&value, data_len); |
| 1274 |
if (ret) { |
| 1275 |
krb5_free_principal (context->context, source); |
| 1276 |
return ret; |
| 1277 |
} |
| 1278 |
krb5_storage_read(sp, value.data, data_len); |
| 1279 |
ret = hdb_value2entry(context->context, &value, &target_ent.entry); |
| 1280 |
krb5_data_free(&value); |
| 1281 |
if (ret) { |
| 1282 |
krb5_free_principal(context->context, source); |
| 1283 |
return ret; |
| 1284 |
} |
| 1285 |
ret = context->db->hdb_store(context->context, context->db, |
| 1286 |
0, &target_ent); |
| 1287 |
hdb_free_entry(context->context, &target_ent); |
| 1288 |
if (ret) { |
| 1289 |
krb5_free_principal(context->context, source); |
| 1290 |
return ret; |
| 1291 |
} |
| 1292 |
ret = context->db->hdb_remove(context->context, context->db, 0, source); |
| 1293 |
krb5_free_principal(context->context, source); |
| 1294 |
|
| 1295 |
return ret; |
| 1296 |
} |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* Add a `modify' operation to the log. |
| 1300 |
*/ |
| 1301 |
kadm5_ret_t |
| 1302 |
kadm5_log_modify(kadm5_server_context *context, |
| 1303 |
hdb_entry *entry, |
| 1304 |
uint32_t mask) |
| 1305 |
{ |
| 1306 |
krb5_storage *sp; |
| 1307 |
kadm5_ret_t ret; |
| 1308 |
krb5_data value; |
| 1309 |
uint32_t len; |
| 1310 |
hdb_entry_ex ent; |
| 1311 |
kadm5_log_context *log_context = &context->log_context; |
| 1312 |
|
| 1313 |
memset(&ent, 0, sizeof(ent)); |
| 1314 |
ent.ctx = 0; |
| 1315 |
ent.free_entry = 0; |
| 1316 |
ent.entry = *entry; |
| 1317 |
|
| 1318 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 1319 |
return context->db->hdb_store(context->context, context->db, |
| 1320 |
HDB_F_REPLACE, &ent); |
| 1321 |
|
| 1322 |
ret = context->db->hdb_store(context->context, context->db, |
| 1323 |
HDB_F_PRECHECK | HDB_F_REPLACE, &ent); |
| 1324 |
if (ret) |
| 1325 |
return ret; |
| 1326 |
|
| 1327 |
sp = krb5_storage_emem(); |
| 1328 |
krb5_data_zero(&value); |
| 1329 |
if (sp == NULL) |
| 1330 |
ret = ENOMEM; |
| 1331 |
if (ret == 0) |
| 1332 |
ret = hdb_entry2value(context->context, entry, &value); |
| 1333 |
if (ret) { |
| 1334 |
krb5_data_free(&value); |
| 1335 |
krb5_storage_free(sp); |
| 1336 |
return ret; |
| 1337 |
} |
| 1338 |
|
| 1339 |
len = value.length + sizeof(len); |
| 1340 |
if (value.length > len || len > INT32_MAX) |
| 1341 |
ret = E2BIG; |
| 1342 |
if (ret == 0) |
| 1343 |
ret = kadm5_log_preamble(context, sp, kadm_modify, |
| 1344 |
log_context->version + 1); |
| 1345 |
if (ret == 0) |
| 1346 |
ret = krb5_store_uint32(sp, len); |
| 1347 |
if (ret == 0) |
| 1348 |
ret = krb5_store_uint32(sp, mask); |
| 1349 |
if (ret == 0) { |
| 1350 |
if (krb5_storage_write(sp, value.data, value.length) != |
| 1351 |
(krb5_ssize_t)value.length) |
| 1352 |
ret = errno; |
| 1353 |
} |
| 1354 |
if (ret == 0) |
| 1355 |
ret = krb5_store_uint32(sp, len); |
| 1356 |
if (ret == 0) |
| 1357 |
ret = kadm5_log_postamble(log_context, sp, |
| 1358 |
log_context->version + 1); |
| 1359 |
if (ret == 0) |
| 1360 |
ret = kadm5_log_flush(context, sp); |
| 1361 |
if (ret == 0) |
| 1362 |
ret = kadm5_log_recover(context, kadm_recover_commit); |
| 1363 |
krb5_data_free(&value); |
| 1364 |
krb5_storage_free(sp); |
| 1365 |
return ret; |
| 1366 |
} |
| 1367 |
|
| 1368 |
/* |
| 1369 |
* Read a `modify' log operation from `sp' and apply it. |
| 1370 |
*/ |
| 1371 |
static kadm5_ret_t |
| 1372 |
kadm5_log_replay_modify(kadm5_server_context *context, |
| 1373 |
uint32_t ver, |
| 1374 |
uint32_t len, |
| 1375 |
krb5_storage *sp) |
| 1376 |
{ |
| 1377 |
krb5_error_code ret; |
| 1378 |
uint32_t mask; |
| 1379 |
krb5_data value; |
| 1380 |
hdb_entry_ex ent, log_ent; |
| 1381 |
|
| 1382 |
memset(&log_ent, 0, sizeof(log_ent)); |
| 1383 |
|
| 1384 |
ret = krb5_ret_uint32(sp, &mask); |
| 1385 |
if (ret) |
| 1386 |
return ret; |
| 1387 |
len -= 4; |
| 1388 |
ret = krb5_data_alloc (&value, len); |
| 1389 |
if (ret) { |
| 1390 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1391 |
return ret; |
| 1392 |
} |
| 1393 |
errno = 0; |
| 1394 |
if (krb5_storage_read (sp, value.data, len) != (krb5_ssize_t)len) { |
| 1395 |
ret = errno ? errno : EIO; |
| 1396 |
return ret; |
| 1397 |
} |
| 1398 |
ret = hdb_value2entry (context->context, &value, &log_ent.entry); |
| 1399 |
krb5_data_free(&value); |
| 1400 |
if (ret) |
| 1401 |
return ret; |
| 1402 |
|
| 1403 |
memset(&ent, 0, sizeof(ent)); |
| 1404 |
ret = context->db->hdb_fetch_kvno(context->context, context->db, |
| 1405 |
log_ent.entry.principal, |
| 1406 |
HDB_F_DECRYPT|HDB_F_ALL_KVNOS| |
| 1407 |
HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent); |
| 1408 |
if (ret) |
| 1409 |
goto out; |
| 1410 |
if (mask & KADM5_PRINC_EXPIRE_TIME) { |
| 1411 |
if (log_ent.entry.valid_end == NULL) { |
| 1412 |
ent.entry.valid_end = NULL; |
| 1413 |
} else { |
| 1414 |
if (ent.entry.valid_end == NULL) { |
| 1415 |
ent.entry.valid_end = malloc(sizeof(*ent.entry.valid_end)); |
| 1416 |
if (ent.entry.valid_end == NULL) { |
| 1417 |
ret = ENOMEM; |
| 1418 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1419 |
goto out; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
*ent.entry.valid_end = *log_ent.entry.valid_end; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
if (mask & KADM5_PW_EXPIRATION) { |
| 1426 |
if (log_ent.entry.pw_end == NULL) { |
| 1427 |
ent.entry.pw_end = NULL; |
| 1428 |
} else { |
| 1429 |
if (ent.entry.pw_end == NULL) { |
| 1430 |
ent.entry.pw_end = malloc(sizeof(*ent.entry.pw_end)); |
| 1431 |
if (ent.entry.pw_end == NULL) { |
| 1432 |
ret = ENOMEM; |
| 1433 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1434 |
goto out; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
*ent.entry.pw_end = *log_ent.entry.pw_end; |
| 1438 |
} |
| 1439 |
} |
| 1440 |
if (mask & KADM5_LAST_PWD_CHANGE) { |
| 1441 |
krb5_warnx (context->context, |
| 1442 |
"Unimplemented mask KADM5_LAST_PWD_CHANGE"); |
| 1443 |
} |
| 1444 |
if (mask & KADM5_ATTRIBUTES) { |
| 1445 |
ent.entry.flags = log_ent.entry.flags; |
| 1446 |
} |
| 1447 |
if (mask & KADM5_MAX_LIFE) { |
| 1448 |
if (log_ent.entry.max_life == NULL) { |
| 1449 |
ent.entry.max_life = NULL; |
| 1450 |
} else { |
| 1451 |
if (ent.entry.max_life == NULL) { |
| 1452 |
ent.entry.max_life = malloc (sizeof(*ent.entry.max_life)); |
| 1453 |
if (ent.entry.max_life == NULL) { |
| 1454 |
ret = ENOMEM; |
| 1455 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1456 |
goto out; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
*ent.entry.max_life = *log_ent.entry.max_life; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
if ((mask & KADM5_MOD_TIME) && (mask & KADM5_MOD_NAME)) { |
| 1463 |
if (ent.entry.modified_by == NULL) { |
| 1464 |
ent.entry.modified_by = malloc(sizeof(*ent.entry.modified_by)); |
| 1465 |
if (ent.entry.modified_by == NULL) { |
| 1466 |
ret = ENOMEM; |
| 1467 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1468 |
goto out; |
| 1469 |
} |
| 1470 |
} else |
| 1471 |
free_Event(ent.entry.modified_by); |
| 1472 |
ret = copy_Event(log_ent.entry.modified_by, ent.entry.modified_by); |
| 1473 |
if (ret) { |
| 1474 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1475 |
goto out; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
if (mask & KADM5_KVNO) { |
| 1479 |
ent.entry.kvno = log_ent.entry.kvno; |
| 1480 |
} |
| 1481 |
if (mask & KADM5_MKVNO) { |
| 1482 |
krb5_warnx(context->context, "Unimplemented mask KADM5_KVNO"); |
| 1483 |
} |
| 1484 |
if (mask & KADM5_AUX_ATTRIBUTES) { |
| 1485 |
krb5_warnx(context->context, |
| 1486 |
"Unimplemented mask KADM5_AUX_ATTRIBUTES"); |
| 1487 |
} |
| 1488 |
if (mask & KADM5_POLICY_CLR) { |
| 1489 |
krb5_warnx(context->context, "Unimplemented mask KADM5_POLICY_CLR"); |
| 1490 |
} |
| 1491 |
if (mask & KADM5_MAX_RLIFE) { |
| 1492 |
if (log_ent.entry.max_renew == NULL) { |
| 1493 |
ent.entry.max_renew = NULL; |
| 1494 |
} else { |
| 1495 |
if (ent.entry.max_renew == NULL) { |
| 1496 |
ent.entry.max_renew = malloc (sizeof(*ent.entry.max_renew)); |
| 1497 |
if (ent.entry.max_renew == NULL) { |
| 1498 |
ret = ENOMEM; |
| 1499 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1500 |
goto out; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
*ent.entry.max_renew = *log_ent.entry.max_renew; |
| 1504 |
} |
| 1505 |
} |
| 1506 |
if (mask & KADM5_LAST_SUCCESS) { |
| 1507 |
krb5_warnx(context->context, "Unimplemented mask KADM5_LAST_SUCCESS"); |
| 1508 |
} |
| 1509 |
if (mask & KADM5_LAST_FAILED) { |
| 1510 |
krb5_warnx(context->context, "Unimplemented mask KADM5_LAST_FAILED"); |
| 1511 |
} |
| 1512 |
if (mask & KADM5_FAIL_AUTH_COUNT) { |
| 1513 |
krb5_warnx(context->context, |
| 1514 |
"Unimplemented mask KADM5_FAIL_AUTH_COUNT"); |
| 1515 |
} |
| 1516 |
if (mask & KADM5_KEY_DATA) { |
| 1517 |
size_t num; |
| 1518 |
size_t i; |
| 1519 |
|
| 1520 |
/* |
| 1521 |
* We don't need to do anything about key history here because |
| 1522 |
* the log entry contains a complete entry, including hdb |
| 1523 |
* extensions. We do need to make sure that KADM5_TL_DATA is in |
| 1524 |
* the mask though, since that's what it takes to update the |
| 1525 |
* extensions (see below). |
| 1526 |
*/ |
| 1527 |
mask |= KADM5_TL_DATA; |
| 1528 |
|
| 1529 |
for (i = 0; i < ent.entry.keys.len; ++i) |
| 1530 |
free_Key(&ent.entry.keys.val[i]); |
| 1531 |
free (ent.entry.keys.val); |
| 1532 |
|
| 1533 |
num = log_ent.entry.keys.len; |
| 1534 |
|
| 1535 |
ent.entry.keys.len = num; |
| 1536 |
ent.entry.keys.val = malloc(len * sizeof(*ent.entry.keys.val)); |
| 1537 |
if (ent.entry.keys.val == NULL) { |
| 1538 |
krb5_set_error_message(context->context, ENOMEM, "out of memory"); |
| 1539 |
ret = ENOMEM; |
| 1540 |
goto out; |
| 1541 |
} |
| 1542 |
for (i = 0; i < ent.entry.keys.len; ++i) { |
| 1543 |
ret = copy_Key(&log_ent.entry.keys.val[i], |
| 1544 |
&ent.entry.keys.val[i]); |
| 1545 |
if (ret) { |
| 1546 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1547 |
goto out; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
} |
| 1551 |
if ((mask & KADM5_TL_DATA) && log_ent.entry.extensions) { |
| 1552 |
HDB_extensions *es = ent.entry.extensions; |
| 1553 |
|
| 1554 |
ent.entry.extensions = calloc(1, sizeof(*ent.entry.extensions)); |
| 1555 |
if (ent.entry.extensions == NULL) |
| 1556 |
goto out; |
| 1557 |
|
| 1558 |
ret = copy_HDB_extensions(log_ent.entry.extensions, |
| 1559 |
ent.entry.extensions); |
| 1560 |
if (ret) { |
| 1561 |
krb5_set_error_message(context->context, ret, "out of memory"); |
| 1562 |
free(ent.entry.extensions); |
| 1563 |
ent.entry.extensions = es; |
| 1564 |
goto out; |
| 1565 |
} |
| 1566 |
if (es) { |
| 1567 |
free_HDB_extensions(es); |
| 1568 |
free(es); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
ret = context->db->hdb_store(context->context, context->db, |
| 1572 |
HDB_F_REPLACE, &ent); |
| 1573 |
out: |
| 1574 |
hdb_free_entry(context->context, &ent); |
| 1575 |
hdb_free_entry(context->context, &log_ent); |
| 1576 |
return ret; |
| 1577 |
} |
| 1578 |
|
| 1579 |
/* |
| 1580 |
* Update the first entry (which should be a `nop'), the "uber-entry". |
| 1581 |
*/ |
| 1582 |
static kadm5_ret_t |
| 1583 |
log_update_uber(kadm5_server_context *context, off_t off) |
| 1584 |
{ |
| 1585 |
kadm5_log_context *log_context = &context->log_context; |
| 1586 |
kadm5_ret_t ret = 0; |
| 1587 |
krb5_storage *sp, *mem_sp; |
| 1588 |
krb5_data data; |
| 1589 |
uint32_t op, len; |
| 1590 |
ssize_t bytes; |
| 1591 |
|
| 1592 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 1593 |
return 0; |
| 1594 |
|
| 1595 |
if (log_context->read_only) |
| 1596 |
return EROFS; |
| 1597 |
|
| 1598 |
krb5_data_zero(&data); |
| 1599 |
|
| 1600 |
mem_sp = krb5_storage_emem(); |
| 1601 |
if (mem_sp == NULL) |
| 1602 |
return ENOMEM; |
| 1603 |
|
| 1604 |
sp = krb5_storage_from_fd(log_context->log_fd); |
| 1605 |
if (sp == NULL) { |
| 1606 |
krb5_storage_free(mem_sp); |
| 1607 |
return ENOMEM; |
| 1608 |
} |
| 1609 |
|
| 1610 |
/* Skip first entry's version and timestamp */ |
| 1611 |
if (krb5_storage_seek(sp, 8, SEEK_SET) == -1) { |
| 1612 |
ret = errno; |
| 1613 |
goto out; |
| 1614 |
} |
| 1615 |
|
| 1616 |
/* If the first entry is not a nop, there's nothing we can do here */ |
| 1617 |
ret = krb5_ret_uint32(sp, &op); |
| 1618 |
if (ret || op != kadm_nop) |
| 1619 |
goto out; |
| 1620 |
|
| 1621 |
/* If the first entry is not a 16-byte nop, ditto */ |
| 1622 |
ret = krb5_ret_uint32(sp, &len); |
| 1623 |
if (ret || len != LOG_UBER_LEN) |
| 1624 |
goto out; |
| 1625 |
|
| 1626 |
/* |
| 1627 |
* Try to make the writes here as close to atomic as possible: a |
| 1628 |
* single write() call. |
| 1629 |
*/ |
| 1630 |
ret = krb5_store_uint64(mem_sp, off); |
| 1631 |
if (ret) |
| 1632 |
goto out; |
| 1633 |
ret = krb5_store_uint32(mem_sp, log_context->last_time); |
| 1634 |
if (ret) |
| 1635 |
goto out; |
| 1636 |
ret = krb5_store_uint32(mem_sp, log_context->version); |
| 1637 |
if (ret) |
| 1638 |
goto out; |
| 1639 |
|
| 1640 |
krb5_storage_to_data(mem_sp, &data); |
| 1641 |
bytes = krb5_storage_write(sp, data.data, data.length); |
| 1642 |
if (bytes < 0) |
| 1643 |
ret = errno; |
| 1644 |
else if (bytes != data.length) |
| 1645 |
ret = EIO; |
| 1646 |
|
| 1647 |
/* |
| 1648 |
* We don't fsync() this write because we can recover if the write |
| 1649 |
* doesn't complete, though for now we don't have code for properly |
| 1650 |
* dealing with the offset not getting written completely. |
| 1651 |
* |
| 1652 |
* We should probably have two copies of the offset so we can use |
| 1653 |
* one copy to verify the other, and when they don't match we could |
| 1654 |
* traverse the whole log forwards, replaying just the last entry. |
| 1655 |
*/ |
| 1656 |
|
| 1657 |
out: |
| 1658 |
if (ret == 0) |
| 1659 |
kadm5_log_signal_master(context); |
| 1660 |
krb5_data_free(&data); |
| 1661 |
krb5_storage_free(sp); |
| 1662 |
krb5_storage_free(mem_sp); |
| 1663 |
if (lseek(log_context->log_fd, off, SEEK_SET) == -1) |
| 1664 |
ret = ret ? ret : errno; |
| 1665 |
|
| 1666 |
return ret; |
| 1667 |
} |
| 1668 |
|
| 1669 |
/* |
| 1670 |
* Add a `nop' operation to the log. Does not close the log. |
| 1671 |
*/ |
| 1672 |
kadm5_ret_t |
| 1673 |
kadm5_log_nop(kadm5_server_context *context, enum kadm_nop_type nop_type) |
| 1674 |
{ |
| 1675 |
krb5_storage *sp; |
| 1676 |
kadm5_ret_t ret; |
| 1677 |
kadm5_log_context *log_context = &context->log_context; |
| 1678 |
off_t off; |
| 1679 |
uint32_t vno = log_context->version; |
| 1680 |
|
| 1681 |
if (strcmp(log_context->log_file, "/dev/null") == 0) |
| 1682 |
return 0; |
| 1683 |
|
| 1684 |
off = lseek(log_context->log_fd, 0, SEEK_CUR); |
| 1685 |
if (off == -1) |
| 1686 |
return errno; |
| 1687 |
|
| 1688 |
sp = krb5_storage_emem(); |
| 1689 |
ret = kadm5_log_preamble(context, sp, kadm_nop, off == 0 ? 0 : vno + 1); |
| 1690 |
if (ret) |
| 1691 |
goto out; |
| 1692 |
|
| 1693 |
if (off == 0) { |
| 1694 |
/* |
| 1695 |
* First entry (uber-entry) gets room for offset of next new |
| 1696 |
* entry and time and version of last entry. |
| 1697 |
*/ |
| 1698 |
ret = krb5_store_uint32(sp, LOG_UBER_LEN); |
| 1699 |
/* These get overwritten with the same values below */ |
| 1700 |
if (ret == 0) |
| 1701 |
ret = krb5_store_uint64(sp, LOG_UBER_SZ); |
| 1702 |
if (ret == 0) |
| 1703 |
ret = krb5_store_uint32(sp, log_context->last_time); |
| 1704 |
if (ret == 0) |
| 1705 |
ret = krb5_store_uint32(sp, vno); |
| 1706 |
if (ret == 0) |
| 1707 |
ret = krb5_store_uint32(sp, LOG_UBER_LEN); |
| 1708 |
} else if (nop_type == kadm_nop_plain) { |
| 1709 |
ret = krb5_store_uint32(sp, 0); |
| 1710 |
if (ret == 0) |
| 1711 |
ret = krb5_store_uint32(sp, 0); |
| 1712 |
} else { |
| 1713 |
ret = krb5_store_uint32(sp, sizeof(uint32_t)); |
| 1714 |
if (ret == 0) |
| 1715 |
ret = krb5_store_uint32(sp, nop_type); |
| 1716 |
if (ret == 0) |
| 1717 |
ret = krb5_store_uint32(sp, sizeof(uint32_t)); |
| 1718 |
} |
| 1719 |
|
| 1720 |
if (ret == 0) |
| 1721 |
ret = kadm5_log_postamble(log_context, sp, off == 0 ? 0 : vno + 1); |
| 1722 |
if (ret == 0) |
| 1723 |
ret = kadm5_log_flush(context, sp); |
| 1724 |
|
| 1725 |
if (ret == 0 && off == 0 && nop_type != kadm_nop_plain) |
| 1726 |
ret = kadm5_log_nop(context, nop_type); |
| 1727 |
|
| 1728 |
if (ret == 0 && off != 0) |
| 1729 |
ret = kadm5_log_recover(context, kadm_recover_commit); |
| 1730 |
|
| 1731 |
out: |
| 1732 |
krb5_storage_free(sp); |
| 1733 |
return ret; |
| 1734 |
} |
| 1735 |
|
| 1736 |
/* |
| 1737 |
* Read a `nop' log operation from `sp' and "apply" it (there's nothing |
| 1738 |
* to do). |
| 1739 |
* |
| 1740 |
* FIXME Actually, if the nop payload is 4 bytes and contains an enum |
| 1741 |
* kadm_nop_type value of kadm_nop_trunc then we should truncate the |
| 1742 |
* log, and if it contains a kadm_nop_close then we should rename a new |
| 1743 |
* log into place. However, this is not implemented yet. |
| 1744 |
*/ |
| 1745 |
static kadm5_ret_t |
| 1746 |
kadm5_log_replay_nop(kadm5_server_context *context, |
| 1747 |
uint32_t ver, |
| 1748 |
uint32_t len, |
| 1749 |
krb5_storage *sp) |
| 1750 |
{ |
| 1751 |
return 0; |
| 1752 |
} |
| 1753 |
|
| 1754 |
struct replay_cb_data { |
| 1755 |
size_t count; |
| 1756 |
uint32_t ver; |
| 1757 |
enum kadm_recover_mode mode; |
| 1758 |
}; |
| 1759 |
|
| 1760 |
|
| 1761 |
/* |
| 1762 |
* Recover or perform the initial commit of an unconfirmed log entry |
| 1763 |
*/ |
| 1764 |
static kadm5_ret_t |
| 1765 |
recover_replay(kadm5_server_context *context, |
| 1766 |
uint32_t ver, time_t timestamp, enum kadm_ops op, |
| 1767 |
uint32_t len, krb5_storage *sp, void *ctx) |
| 1768 |
{ |
| 1769 |
struct replay_cb_data *data = ctx; |
| 1770 |
kadm5_ret_t ret; |
| 1771 |
off_t off; |
| 1772 |
|
| 1773 |
/* On initial commit there must be just one pending unconfirmed entry */ |
| 1774 |
if (data->count > 0 && data->mode == kadm_recover_commit) |
| 1775 |
return KADM5_LOG_CORRUPT; |
| 1776 |
|
| 1777 |
/* We're at the start of the payload; compute end of entry offset */ |
| 1778 |
off = krb5_storage_seek(sp, 0, SEEK_CUR) + len + LOG_TRAILER_SZ; |
| 1779 |
|
| 1780 |
/* We cannot perform log recovery on LDAP and such backends */ |
| 1781 |
if (data->mode == kadm_recover_replay && |
| 1782 |
(context->db->hdb_capability_flags & HDB_CAP_F_SHARED_DIRECTORY)) |
| 1783 |
ret = 0; |
| 1784 |
else |
| 1785 |
ret = kadm5_log_replay(context, op, ver, len, sp); |
| 1786 |
switch (ret) { |
| 1787 |
case HDB_ERR_NOENTRY: |
| 1788 |
case HDB_ERR_EXISTS: |
| 1789 |
if (data->mode != kadm_recover_replay) |
| 1790 |
return ret; |
| 1791 |
case 0: |
| 1792 |
break; |
| 1793 |
case KADM5_LOG_CORRUPT: |
| 1794 |
return -1; |
| 1795 |
default: |
| 1796 |
krb5_warn(context->context, ret, "unexpected error while replaying"); |
| 1797 |
return -1; |
| 1798 |
} |
| 1799 |
data->count++; |
| 1800 |
data->ver = ver; |
| 1801 |
|
| 1802 |
/* |
| 1803 |
* With replay we may be making multiple HDB changes. We must sync the |
| 1804 |
* confirmation of each one before moving on to the next. Otherwise, we |
| 1805 |
* might attempt to replay multiple already applied updates, and this may |
| 1806 |
* introduce unintended intermediate states or fail to yield the same final |
| 1807 |
* result. |
| 1808 |
*/ |
| 1809 |
kadm5_log_set_version(context, ver); |
| 1810 |
ret = log_update_uber(context, off); |
| 1811 |
if (ret == 0 && data->mode != kadm_recover_commit) |
| 1812 |
ret = krb5_storage_fsync(sp); |
| 1813 |
return ret; |
| 1814 |
} |
| 1815 |
|
| 1816 |
|
| 1817 |
kadm5_ret_t |
| 1818 |
kadm5_log_recover(kadm5_server_context *context, enum kadm_recover_mode mode) |
| 1819 |
{ |
| 1820 |
kadm5_ret_t ret; |
| 1821 |
krb5_storage *sp; |
| 1822 |
struct replay_cb_data replay_data; |
| 1823 |
|
| 1824 |
replay_data.count = 0; |
| 1825 |
replay_data.ver = 0; |
| 1826 |
replay_data.mode = mode; |
| 1827 |
|
| 1828 |
sp = krb5_storage_from_fd(context->log_context.log_fd); |
| 1829 |
if (sp == NULL) |
| 1830 |
return errno ? errno : EIO; |
| 1831 |
ret = kadm5_log_goto_end(context, sp); |
| 1832 |
|
| 1833 |
if (ret == 0) |
| 1834 |
ret = kadm5_log_foreach(context, kadm_forward | kadm_unconfirmed, |
| 1835 |
NULL, recover_replay, &replay_data); |
| 1836 |
if (ret == 0 && mode == kadm_recover_commit && replay_data.count != 1) |
| 1837 |
ret = KADM5_LOG_CORRUPT; |
| 1838 |
krb5_storage_free(sp); |
| 1839 |
return ret; |
| 1840 |
} |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Call `func' for each log record in the log in `context'. |
| 1844 |
* |
| 1845 |
* `func' is optional. |
| 1846 |
* |
| 1847 |
* If `func' returns -1 then log traversal terminates and this returns 0. |
| 1848 |
* Otherwise `func''s return is returned if there are no other errors. |
| 1849 |
*/ |
| 1850 |
kadm5_ret_t |
| 1851 |
kadm5_log_foreach(kadm5_server_context *context, |
| 1852 |
enum kadm_iter_opts iter_opts, |
| 1853 |
off_t *off_lastp, |
| 1854 |
kadm5_ret_t (*func)(kadm5_server_context *server_context, |
| 1855 |
uint32_t ver, time_t timestamp, |
| 1856 |
enum kadm_ops op, uint32_t len, |
| 1857 |
krb5_storage *sp, void *ctx), |
| 1858 |
void *ctx) |
| 1859 |
{ |
| 1860 |
kadm5_ret_t ret = 0; |
| 1861 |
int fd = context->log_context.log_fd; |
| 1862 |
krb5_storage *sp; |
| 1863 |
off_t off_last; |
| 1864 |
off_t this_entry = 0; |
| 1865 |
off_t log_end = 0; |
| 1866 |
|
| 1867 |
if (strcmp(context->log_context.log_file, "/dev/null") == 0) |
| 1868 |
return 0; |
| 1869 |
|
| 1870 |
if (off_lastp == NULL) |
| 1871 |
off_lastp = &off_last; |
| 1872 |
*off_lastp = -1; |
| 1873 |
|
| 1874 |
if (((iter_opts & kadm_forward) && (iter_opts & kadm_backward)) || |
| 1875 |
(!(iter_opts & kadm_confirmed) && !(iter_opts & kadm_unconfirmed))) |
| 1876 |
return EINVAL; |
| 1877 |
|
| 1878 |
if ((iter_opts & kadm_forward) && (iter_opts & kadm_confirmed) && |
| 1879 |
(iter_opts & kadm_unconfirmed)) { |
| 1880 |
/* |
| 1881 |
* We want to traverse all log entries, confirmed or not, from |
| 1882 |
* the start, then there's no need to kadm5_log_goto_end() |
| 1883 |
* -- no reason to try to find the end. |
| 1884 |
*/ |
| 1885 |
sp = krb5_storage_from_fd(fd); |
| 1886 |
if (sp == NULL) |
| 1887 |
return errno ? errno : ENOMEM; |
| 1888 |
|
| 1889 |
log_end = krb5_storage_seek(sp, 0, SEEK_END); |
| 1890 |
if (log_end == -1 || |
| 1891 |
krb5_storage_seek(sp, 0, SEEK_SET) == -1) { |
| 1892 |
ret = errno; |
| 1893 |
krb5_storage_free(sp); |
| 1894 |
return ret; |
| 1895 |
} |
| 1896 |
} else { |
| 1897 |
/* Get the end of the log based on the uber entry */ |
| 1898 |
sp = krb5_storage_from_fd(fd); |
| 1899 |
if (sp == NULL) |
| 1900 |
return errno ? errno : ENOMEM; |
| 1901 |
ret = kadm5_log_goto_end(context, sp); |
| 1902 |
if (ret != 0) |
| 1903 |
return ret; |
| 1904 |
log_end = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1905 |
} |
| 1906 |
|
| 1907 |
*off_lastp = log_end; |
| 1908 |
|
| 1909 |
if ((iter_opts & kadm_forward) && (iter_opts & kadm_confirmed)) { |
| 1910 |
/* Start at the beginning */ |
| 1911 |
if (krb5_storage_seek(sp, 0, SEEK_SET) == -1) { |
| 1912 |
ret = errno; |
| 1913 |
krb5_storage_free(sp); |
| 1914 |
return ret; |
| 1915 |
} |
| 1916 |
} else if ((iter_opts & kadm_backward) && (iter_opts & kadm_unconfirmed)) { |
| 1917 |
/* |
| 1918 |
* We're at the confirmed end but need to be at the unconfirmed |
| 1919 |
* end. Skip forward to the real end, re-entering to do it. |
| 1920 |
*/ |
| 1921 |
ret = kadm5_log_foreach(context, kadm_forward | kadm_unconfirmed, |
| 1922 |
&log_end, NULL, NULL); |
| 1923 |
if (ret) |
| 1924 |
return ret; |
| 1925 |
if (krb5_storage_seek(sp, log_end, SEEK_SET) == -1) { |
| 1926 |
ret = errno; |
| 1927 |
krb5_storage_free(sp); |
| 1928 |
return ret; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
for (;;) { |
| 1933 |
uint32_t ver, ver2, len, len2; |
| 1934 |
uint32_t tstamp; |
| 1935 |
time_t timestamp; |
| 1936 |
enum kadm_ops op; |
| 1937 |
|
| 1938 |
if ((iter_opts & kadm_backward)) { |
| 1939 |
off_t o; |
| 1940 |
|
| 1941 |
o = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1942 |
if (o == 0 || |
| 1943 |
((iter_opts & kadm_unconfirmed) && o <= *off_lastp)) |
| 1944 |
break; |
| 1945 |
ret = kadm5_log_previous(context->context, sp, &ver, |
| 1946 |
×tamp, &op, &len); |
| 1947 |
if (ret) |
| 1948 |
break; |
| 1949 |
|
| 1950 |
/* Offset is now at payload of current entry */ |
| 1951 |
|
| 1952 |
o = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1953 |
if (o == -1) { |
| 1954 |
ret = errno; |
| 1955 |
break; |
| 1956 |
} |
| 1957 |
this_entry = o - LOG_HEADER_SZ; |
| 1958 |
if (this_entry < 0) { |
| 1959 |
ret = KADM5_LOG_CORRUPT; |
| 1960 |
break; |
| 1961 |
} |
| 1962 |
} else { |
| 1963 |
/* Offset is now at start of current entry, read header */ |
| 1964 |
this_entry = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 1965 |
if (!(iter_opts & kadm_unconfirmed) && this_entry == log_end) |
| 1966 |
break; |
| 1967 |
ret = get_header(sp, LOG_NOPEEK, &ver, &tstamp, &op, &len); |
| 1968 |
if (ret == HEIM_ERR_EOF) { |
| 1969 |
ret = 0; |
| 1970 |
break; |
| 1971 |
} |
| 1972 |
timestamp = tstamp; |
| 1973 |
if (ret) |
| 1974 |
break; |
| 1975 |
/* Offset is now at payload of current entry */ |
| 1976 |
} |
| 1977 |
|
| 1978 |
/* Validate trailer before calling the callback */ |
| 1979 |
if (krb5_storage_seek(sp, len, SEEK_CUR) == -1) { |
| 1980 |
ret = errno; |
| 1981 |
break; |
| 1982 |
} |
| 1983 |
|
| 1984 |
ret = krb5_ret_uint32(sp, &len2); |
| 1985 |
if (ret) |
| 1986 |
break; |
| 1987 |
ret = krb5_ret_uint32(sp, &ver2); |
| 1988 |
if (ret) |
| 1989 |
break; |
| 1990 |
if (len != len2 || ver != ver2) { |
| 1991 |
ret = KADM5_LOG_CORRUPT; |
| 1992 |
break; |
| 1993 |
} |
| 1994 |
|
| 1995 |
/* Rewind to start of payload and call callback if we have one */ |
| 1996 |
if (krb5_storage_seek(sp, this_entry + LOG_HEADER_SZ, |
| 1997 |
SEEK_SET) == -1) { |
| 1998 |
ret = errno; |
| 1999 |
break; |
| 2000 |
} |
| 2001 |
|
| 2002 |
if (func != NULL) { |
| 2003 |
ret = (*func)(context, ver, timestamp, op, len, sp, ctx); |
| 2004 |
if (ret) { |
| 2005 |
/* Callback signals desire to stop by returning -1 */ |
| 2006 |
if (ret == -1) |
| 2007 |
ret = 0; |
| 2008 |
break; |
| 2009 |
} |
| 2010 |
} |
| 2011 |
if ((iter_opts & kadm_forward)) { |
| 2012 |
off_t o; |
| 2013 |
|
| 2014 |
o = krb5_storage_seek(sp, this_entry+LOG_WRAPPER_SZ+len, SEEK_SET); |
| 2015 |
if (o == -1) { |
| 2016 |
ret = errno; |
| 2017 |
break; |
| 2018 |
} |
| 2019 |
if (o > log_end) |
| 2020 |
*off_lastp = o; |
| 2021 |
} else if ((iter_opts & kadm_backward)) { |
| 2022 |
/* |
| 2023 |
* Rewind to the start of this entry so kadm5_log_previous() |
| 2024 |
* can find the previous one. |
| 2025 |
*/ |
| 2026 |
if (krb5_storage_seek(sp, this_entry, SEEK_SET) == -1) { |
| 2027 |
ret = errno; |
| 2028 |
break; |
| 2029 |
} |
| 2030 |
} |
| 2031 |
} |
| 2032 |
if ((ret == HEIM_ERR_EOF || ret == KADM5_LOG_CORRUPT) && |
| 2033 |
(iter_opts & kadm_forward) && |
| 2034 |
context->log_context.lock_mode == LOCK_EX) { |
| 2035 |
/* |
| 2036 |
* Truncate partially written last log entry so we can write |
| 2037 |
* again. |
| 2038 |
*/ |
| 2039 |
ret = krb5_storage_truncate(sp, this_entry); |
| 2040 |
if (ret == 0 && |
| 2041 |
krb5_storage_seek(sp, this_entry, SEEK_SET) == -1) |
| 2042 |
ret = errno; |
| 2043 |
krb5_warnx(context->context, "Truncating log at partial or " |
| 2044 |
"corrupt %s entry", |
| 2045 |
this_entry > log_end ? "unconfirmed" : "confirmed"); |
| 2046 |
} |
| 2047 |
krb5_storage_free(sp); |
| 2048 |
return ret; |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* |
| 2052 |
* Go to the first record, which, if we have an uber record, will be |
| 2053 |
* the second record. |
| 2054 |
*/ |
| 2055 |
kadm5_ret_t |
| 2056 |
kadm5_log_goto_first(kadm5_server_context *server_context, krb5_storage *sp) |
| 2057 |
{ |
| 2058 |
enum kadm_ops op; |
| 2059 |
uint32_t ver, len; |
| 2060 |
kadm5_ret_t ret; |
| 2061 |
|
| 2062 |
if (krb5_storage_seek(sp, 0, SEEK_SET) == -1) |
| 2063 |
return KADM5_LOG_CORRUPT; |
| 2064 |
|
| 2065 |
ret = get_header(sp, LOG_DOPEEK, &ver, NULL, &op, &len); |
| 2066 |
if (ret) |
| 2067 |
return ret; |
| 2068 |
if (op == kadm_nop && len == LOG_UBER_LEN && seek_next(sp) == -1) |
| 2069 |
return KADM5_LOG_CORRUPT; |
| 2070 |
return 0; |
| 2071 |
} |
| 2072 |
|
| 2073 |
/* |
| 2074 |
* Go to end of log. |
| 2075 |
*/ |
| 2076 |
kadm5_ret_t |
| 2077 |
kadm5_log_goto_end(kadm5_server_context *server_context, krb5_storage *sp) |
| 2078 |
{ |
| 2079 |
krb5_error_code ret = 0; |
| 2080 |
enum kadm_ops op; |
| 2081 |
uint32_t ver, len; |
| 2082 |
uint32_t tstamp; |
| 2083 |
uint64_t off; |
| 2084 |
|
| 2085 |
if (krb5_storage_seek(sp, 0, SEEK_SET) == -1) |
| 2086 |
return errno; |
| 2087 |
ret = get_header(sp, LOG_NOPEEK, &ver, &tstamp, &op, &len); |
| 2088 |
if (ret == HEIM_ERR_EOF) { |
| 2089 |
(void) krb5_storage_seek(sp, 0, SEEK_SET); |
| 2090 |
return 0; |
| 2091 |
} |
| 2092 |
if (ret == KADM5_LOG_CORRUPT) |
| 2093 |
goto truncate; |
| 2094 |
if (ret) |
| 2095 |
return ret; |
| 2096 |
|
| 2097 |
if (op == kadm_nop && len == LOG_UBER_LEN) { |
| 2098 |
/* New style log */ |
| 2099 |
ret = krb5_ret_uint64(sp, &off); |
| 2100 |
if (ret) |
| 2101 |
goto truncate; |
| 2102 |
|
| 2103 |
if (krb5_storage_seek(sp, off, SEEK_SET) == -1) |
| 2104 |
return ret; |
| 2105 |
|
| 2106 |
if (off >= LOG_UBER_SZ) { |
| 2107 |
ret = get_version_prev(sp, &ver, NULL); |
| 2108 |
if (ret == 0) |
| 2109 |
return 0; |
| 2110 |
} |
| 2111 |
/* Invalid offset in uber entry */ |
| 2112 |
goto truncate; |
| 2113 |
} |
| 2114 |
|
| 2115 |
/* Old log with no uber entry */ |
| 2116 |
if (krb5_storage_seek(sp, 0, SEEK_END) == -1) { |
| 2117 |
static int warned = 0; |
| 2118 |
if (!warned) { |
| 2119 |
warned = 1; |
| 2120 |
krb5_warnx(server_context->context, |
| 2121 |
"Old log found; truncate it to upgrade"); |
| 2122 |
} |
| 2123 |
} |
| 2124 |
ret = get_version_prev(sp, &ver, NULL); |
| 2125 |
if (ret) |
| 2126 |
goto truncate; |
| 2127 |
return 0; |
| 2128 |
|
| 2129 |
truncate: |
| 2130 |
/* If we can, truncate */ |
| 2131 |
if (server_context->log_context.lock_mode == LOCK_EX) { |
| 2132 |
ret = kadm5_log_reinit(server_context, 0); |
| 2133 |
if (ret == 0) { |
| 2134 |
krb5_warn(server_context->context, ret, |
| 2135 |
"Invalid log; truncating to recover"); |
| 2136 |
if (krb5_storage_seek(sp, 0, SEEK_END) >= 0) |
| 2137 |
return 0; |
| 2138 |
} |
| 2139 |
} |
| 2140 |
ret = KADM5_LOG_CORRUPT; |
| 2141 |
krb5_warn(server_context->context, ret, |
| 2142 |
"Invalid log; truncate to recover"); |
| 2143 |
return ret; |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* |
| 2147 |
* Return the next log entry. |
| 2148 |
* |
| 2149 |
* The pointer in `sp' is assumed to be at the end of an entry. On success, |
| 2150 |
* the `sp' pointer is set to the next entry (not the data portion). In case |
| 2151 |
* of error, it's not changed at all. |
| 2152 |
*/ |
| 2153 |
kadm5_ret_t |
| 2154 |
kadm5_log_next(krb5_context context, |
| 2155 |
krb5_storage *sp, |
| 2156 |
uint32_t *verp, |
| 2157 |
time_t *tstampp, |
| 2158 |
enum kadm_ops *opp, |
| 2159 |
uint32_t *lenp) |
| 2160 |
{ |
| 2161 |
uint32_t len = 0; |
| 2162 |
uint32_t len2 = 0; |
| 2163 |
uint32_t ver = verp ? *verp : 0; |
| 2164 |
uint32_t ver2; |
| 2165 |
uint32_t tstamp = tstampp ? *tstampp : 0; |
| 2166 |
enum kadm_ops op = kadm_nop; |
| 2167 |
off_t off = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 2168 |
kadm5_ret_t ret = get_header(sp, LOG_NOPEEK, &ver, &tstamp, &op, &len); |
| 2169 |
|
| 2170 |
/* Validate the trailer */ |
| 2171 |
if (ret == 0 && krb5_storage_seek(sp, len, SEEK_CUR) == -1) |
| 2172 |
ret = errno; |
| 2173 |
|
| 2174 |
if (ret == 0) |
| 2175 |
ret = krb5_ret_uint32(sp, &len2); |
| 2176 |
if (ret == 0) |
| 2177 |
ret = krb5_ret_uint32(sp, &ver2); |
| 2178 |
if (ret == 0 && (len != len2 || ver != ver2)) |
| 2179 |
ret = KADM5_LOG_CORRUPT; |
| 2180 |
if (ret != 0) { |
| 2181 |
(void) krb5_storage_seek(sp, off, SEEK_SET); |
| 2182 |
return ret; |
| 2183 |
} |
| 2184 |
|
| 2185 |
if (verp) |
| 2186 |
*verp = ver; |
| 2187 |
if (tstampp) |
| 2188 |
*tstampp = tstamp; |
| 2189 |
if (opp) |
| 2190 |
*opp = op; |
| 2191 |
if (lenp) |
| 2192 |
*lenp = len; |
| 2193 |
return 0; |
| 2194 |
} |
| 2195 |
|
| 2196 |
/* |
| 2197 |
* Return previous log entry. |
| 2198 |
* |
| 2199 |
* The pointer in `sp' is assumed to be at the top of the entry after |
| 2200 |
* previous entry (e.g., at EOF). On success, the `sp' pointer is set to |
| 2201 |
* data portion of previous entry. In case of error, it's not changed |
| 2202 |
* at all. |
| 2203 |
*/ |
| 2204 |
kadm5_ret_t |
| 2205 |
kadm5_log_previous(krb5_context context, |
| 2206 |
krb5_storage *sp, |
| 2207 |
uint32_t *verp, |
| 2208 |
time_t *tstampp, |
| 2209 |
enum kadm_ops *opp, |
| 2210 |
uint32_t *lenp) |
| 2211 |
{ |
| 2212 |
krb5_error_code ret; |
| 2213 |
off_t oldoff; |
| 2214 |
uint32_t ver2, len2; |
| 2215 |
uint32_t tstamp; |
| 2216 |
|
| 2217 |
oldoff = krb5_storage_seek(sp, 0, SEEK_CUR); |
| 2218 |
if (oldoff == -1) |
| 2219 |
goto log_corrupt; |
| 2220 |
|
| 2221 |
/* This reads the physical version of the uber record */ |
| 2222 |
if (seek_prev(sp, verp, lenp) == -1) |
| 2223 |
goto log_corrupt; |
| 2224 |
|
| 2225 |
ret = get_header(sp, LOG_NOPEEK, &ver2, &tstamp, opp, &len2); |
| 2226 |
if (ret) { |
| 2227 |
(void) krb5_storage_seek(sp, oldoff, SEEK_SET); |
| 2228 |
return ret; |
| 2229 |
} |
| 2230 |
if (tstampp) |
| 2231 |
*tstampp = tstamp; |
| 2232 |
if (ver2 != *verp || len2 != *lenp) |
| 2233 |
goto log_corrupt; |
| 2234 |
|
| 2235 |
return 0; |
| 2236 |
|
| 2237 |
log_corrupt: |
| 2238 |
(void) krb5_storage_seek(sp, oldoff, SEEK_SET); |
| 2239 |
return KADM5_LOG_CORRUPT; |
| 2240 |
} |
| 2241 |
|
| 2242 |
/* |
| 2243 |
* Replay a record from the log |
| 2244 |
*/ |
| 2245 |
|
| 2246 |
kadm5_ret_t |
| 2247 |
kadm5_log_replay(kadm5_server_context *context, |
| 2248 |
enum kadm_ops op, |
| 2249 |
uint32_t ver, |
| 2250 |
uint32_t len, |
| 2251 |
krb5_storage *sp) |
| 2252 |
{ |
| 2253 |
switch (op) { |
| 2254 |
case kadm_create : |
| 2255 |
return kadm5_log_replay_create(context, ver, len, sp); |
| 2256 |
case kadm_delete : |
| 2257 |
return kadm5_log_replay_delete(context, ver, len, sp); |
| 2258 |
case kadm_rename : |
| 2259 |
return kadm5_log_replay_rename(context, ver, len, sp); |
| 2260 |
case kadm_modify : |
| 2261 |
return kadm5_log_replay_modify(context, ver, len, sp); |
| 2262 |
case kadm_nop : |
| 2263 |
return kadm5_log_replay_nop(context, ver, len, sp); |
| 2264 |
default : |
| 2265 |
/* |
| 2266 |
* FIXME This default arm makes it difficult to add new kadm_ops |
| 2267 |
* values. |
| 2268 |
*/ |
| 2269 |
krb5_set_error_message(context->context, KADM5_FAILURE, |
| 2270 |
"Unsupported replay op %d", (int)op); |
| 2271 |
(void) krb5_storage_seek(sp, len, SEEK_CUR); |
| 2272 |
return KADM5_FAILURE; |
| 2273 |
} |
| 2274 |
} |
| 2275 |
|
| 2276 |
struct load_entries_data { |
| 2277 |
krb5_data *entries; |
| 2278 |
unsigned char *p; |
| 2279 |
uint32_t first; |
| 2280 |
uint32_t last; |
| 2281 |
size_t bytes; |
| 2282 |
size_t nentries; |
| 2283 |
size_t maxbytes; |
| 2284 |
size_t maxentries; |
| 2285 |
}; |
| 2286 |
|
| 2287 |
|
| 2288 |
/* |
| 2289 |
* Prepend one entry with header and trailer to the entry buffer, stopping when |
| 2290 |
* we've reached either of the byte or entry-count limits (if non-zero). |
| 2291 |
* |
| 2292 |
* This is a two-pass algorithm: |
| 2293 |
* |
| 2294 |
* In the first pass, when entries->entries == NULL, we compute the space |
| 2295 |
* required, and count the entries that fit up from zero. |
| 2296 |
* |
| 2297 |
* In the second pass we fill the buffer, and count the entries back down to |
| 2298 |
* zero. The space used must be an exact fit, and the number of entries must |
| 2299 |
* reach zero at that point or an error is returned. |
| 2300 |
* |
| 2301 |
* The caller MUST check that entries->nentries == 0 at the end of the second |
| 2302 |
* pass. |
| 2303 |
*/ |
| 2304 |
static kadm5_ret_t |
| 2305 |
load_entries_cb(kadm5_server_context *server_context, |
| 2306 |
uint32_t ver, |
| 2307 |
time_t timestamp, |
| 2308 |
enum kadm_ops op, |
| 2309 |
uint32_t len, |
| 2310 |
krb5_storage *sp, |
| 2311 |
void *ctx) |
| 2312 |
{ |
| 2313 |
struct load_entries_data *entries = ctx; |
| 2314 |
kadm5_ret_t ret; |
| 2315 |
ssize_t bytes; |
| 2316 |
size_t entry_len = len + LOG_WRAPPER_SZ; |
| 2317 |
unsigned char *base; |
| 2318 |
|
| 2319 |
if (entries->entries == NULL) { |
| 2320 |
size_t total = entries->bytes + entry_len; |
| 2321 |
|
| 2322 |
/* |
| 2323 |
* First run: find the size of krb5_data buffer needed. |
| 2324 |
* |
| 2325 |
* If the log was huge we'd have to perhaps open a temp file for this. |
| 2326 |
* For now KISS. |
| 2327 |
*/ |
| 2328 |
if ((op == kadm_nop && entry_len == LOG_UBER_SZ) || |
| 2329 |
entry_len < len /*overflow?*/ || |
| 2330 |
(entries->maxbytes > 0 && total > entries->maxbytes) || |
| 2331 |
total < entries->bytes /*overflow?*/ || |
| 2332 |
(entries->maxentries > 0 && entries->nentries == entries->maxentries)) |
| 2333 |
return -1; /* stop iteration */ |
| 2334 |
entries->bytes = total; |
| 2335 |
entries->first = ver; |
| 2336 |
if (entries->nentries++ == 0) |
| 2337 |
entries->last = ver; |
| 2338 |
return 0; |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* Second run: load the data into memory */ |
| 2342 |
base = (unsigned char *)entries->entries->data; |
| 2343 |
if (entries->p - base < entry_len && entries->p != base) { |
| 2344 |
/* |
| 2345 |
* This can't happen normally: we stop the log record iteration |
| 2346 |
* above before we get here. This could happen if someone wrote |
| 2347 |
* garbage to the log while we were traversing it. We return an |
| 2348 |
* error instead of asserting. |
| 2349 |
*/ |
| 2350 |
return KADM5_LOG_CORRUPT; |
| 2351 |
} |
| 2352 |
|
| 2353 |
/* |
| 2354 |
* sp here is a krb5_storage_from_fd() of the log file, and the |
| 2355 |
* offset pointer points at the current log record payload. |
| 2356 |
* |
| 2357 |
* Seek back to the start of the record poayload so we can read the |
| 2358 |
* whole record. |
| 2359 |
*/ |
| 2360 |
if (krb5_storage_seek(sp, -LOG_HEADER_SZ, SEEK_CUR) == -1) |
| 2361 |
return errno; |
| 2362 |
|
| 2363 |
/* |
| 2364 |
* We read the header, payload, and trailer into the buffer we have, that |
| 2365 |
* many bytes before the previous record we read. |
| 2366 |
*/ |
| 2367 |
errno = 0; |
| 2368 |
bytes = krb5_storage_read(sp, entries->p - entry_len, entry_len); |
| 2369 |
ret = errno; |
| 2370 |
if (bytes < 0 || bytes != entry_len) |
| 2371 |
return ret ? ret : EIO; |
| 2372 |
|
| 2373 |
entries->first = ver; |
| 2374 |
--entries->nentries; |
| 2375 |
entries->p -= entry_len; |
| 2376 |
return (entries->p == base) ? -1 : 0; |
| 2377 |
} |
| 2378 |
|
| 2379 |
|
| 2380 |
/* |
| 2381 |
* Serialize a tail fragment of the log as a krb5_data, this is constrained to |
| 2382 |
* at most `maxbytes' bytes and to at most `maxentries' entries if not zero. |
| 2383 |
*/ |
| 2384 |
static kadm5_ret_t |
| 2385 |
load_entries(kadm5_server_context *context, krb5_data *p, |
| 2386 |
size_t maxentries, size_t maxbytes, |
| 2387 |
uint32_t *first, uint32_t *last) |
| 2388 |
{ |
| 2389 |
struct load_entries_data entries; |
| 2390 |
kadm5_ret_t ret; |
| 2391 |
unsigned char *base; |
| 2392 |
|
| 2393 |
krb5_data_zero(p); |
| 2394 |
|
| 2395 |
*first = 0; |
| 2396 |
|
| 2397 |
memset(&entries, 0, sizeof(entries)); |
| 2398 |
entries.entries = NULL; |
| 2399 |
entries.p = NULL; |
| 2400 |
entries.maxentries = maxentries; |
| 2401 |
entries.maxbytes = maxbytes; |
| 2402 |
|
| 2403 |
/* Figure out how many bytes it will take */ |
| 2404 |
ret = kadm5_log_foreach(context, kadm_backward | kadm_confirmed, |
| 2405 |
NULL, load_entries_cb, &entries); |
| 2406 |
if (ret) |
| 2407 |
return ret; |
| 2408 |
|
| 2409 |
/* |
| 2410 |
* If no entries fit our limits, we do not truncate, instead the caller can |
| 2411 |
* call kadm5_log_reinit() if desired. |
| 2412 |
*/ |
| 2413 |
if (entries.bytes == 0) |
| 2414 |
return 0; |
| 2415 |
|
| 2416 |
ret = krb5_data_alloc(p, entries.bytes); |
| 2417 |
if (ret) |
| 2418 |
return ret; |
| 2419 |
|
| 2420 |
*first = entries.first; |
| 2421 |
*last = entries.last; |
| 2422 |
entries.entries = p; |
| 2423 |
base = (unsigned char *)entries.entries->data; |
| 2424 |
entries.p = base + entries.bytes; |
| 2425 |
|
| 2426 |
ret = kadm5_log_foreach(context, kadm_backward | kadm_confirmed, |
| 2427 |
NULL, load_entries_cb, &entries); |
| 2428 |
if (ret == 0 && |
| 2429 |
(entries.nentries || entries.p != base || entries.first != *first)) |
| 2430 |
ret = KADM5_LOG_CORRUPT; |
| 2431 |
if (ret) |
| 2432 |
krb5_data_free(p); |
| 2433 |
return ret; |
| 2434 |
} |
| 2435 |
|
| 2436 |
/* |
| 2437 |
* Truncate the log, retaining at most `keep' entries and at most `maxbytes'. |
| 2438 |
* If `maxbytes' is zero, keep at most the default log size limit. |
| 2439 |
*/ |
| 2440 |
kadm5_ret_t |
| 2441 |
kadm5_log_truncate(kadm5_server_context *context, size_t keep, size_t maxbytes) |
| 2442 |
{ |
| 2443 |
kadm5_ret_t ret; |
| 2444 |
uint32_t first, last, last_tstamp; |
| 2445 |
time_t now = time(NULL); |
| 2446 |
krb5_data entries; |
| 2447 |
krb5_storage *sp; |
| 2448 |
ssize_t bytes; |
| 2449 |
uint64_t sz; |
| 2450 |
off_t off; |
| 2451 |
|
| 2452 |
if (maxbytes == 0) |
| 2453 |
maxbytes = get_max_log_size(context->context); |
| 2454 |
|
| 2455 |
if (strcmp(context->log_context.log_file, "/dev/null") == 0) |
| 2456 |
return 0; |
| 2457 |
|
| 2458 |
if (context->log_context.read_only) |
| 2459 |
return EROFS; |
| 2460 |
|
| 2461 |
/* Get the desired records. */ |
| 2462 |
krb5_data_zero(&entries); |
| 2463 |
ret = load_entries(context, &entries, keep, maxbytes, &first, &last); |
| 2464 |
if (ret) |
| 2465 |
return ret; |
| 2466 |
|
| 2467 |
if (first == 0) { |
| 2468 |
/* |
| 2469 |
* No records found/fit within resource limits. The caller should call |
| 2470 |
* kadm5_log_reinit(context) to truly truncate and reset the log to |
| 2471 |
* version 0, else call again with better limits. |
| 2472 |
*/ |
| 2473 |
krb5_data_free(&entries); |
| 2474 |
return EINVAL; |
| 2475 |
} |
| 2476 |
|
| 2477 |
/* Check that entries.length won't overflow off_t */ |
| 2478 |
sz = LOG_UBER_SZ + entries.length; |
| 2479 |
off = (off_t)sz; |
| 2480 |
if (off < 0 || off != sz || sz < entries.length) { |
| 2481 |
krb5_data_free(&entries); |
| 2482 |
return EOVERFLOW; /* caller should ask for fewer entries */ |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* Truncate to zero size and seek to zero offset */ |
| 2486 |
if (ftruncate(context->log_context.log_fd, 0) < 0 || |
| 2487 |
lseek(context->log_context.log_fd, 0, SEEK_SET) < 0) { |
| 2488 |
krb5_data_free(&entries); |
| 2489 |
return errno; |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* |
| 2493 |
* Write the uber record and then the records loaded. Confirm the entries |
| 2494 |
* after writing them. |
| 2495 |
* |
| 2496 |
* If we crash then the log may not have all the entries we want, and |
| 2497 |
* replaying only some of the entries will leave us in a bad state. |
| 2498 |
* Additionally, we don't have mathematical proof that replaying the last |
| 2499 |
* N>1 entries is always idempotent. And though we believe we can make |
| 2500 |
* such replays idempotent, they would still leave the HDB with |
| 2501 |
* intermediate states that would not have occurred on the master. |
| 2502 |
* |
| 2503 |
* By initially setting the offset in the uber record to 0, the log will be |
| 2504 |
* seen as invalid should we crash here, thus the only |
| 2505 |
* harm will be that we'll reinitialize the log and force full props. |
| 2506 |
* |
| 2507 |
* We can't use the normal kadm5_log_*() machinery for this because |
| 2508 |
* we must set specific version numbers and timestamps. To keep |
| 2509 |
* things simple we don't try to do a single atomic write here as we |
| 2510 |
* do in kadm5_log_flush(). |
| 2511 |
* |
| 2512 |
* We really do want to keep the new first entry's version and |
| 2513 |
* timestamp so we don't trip up iprop. |
| 2514 |
* |
| 2515 |
* Keep this in sync with kadm5_log_nop(). |
| 2516 |
*/ |
| 2517 |
sp = krb5_storage_from_fd(context->log_context.log_fd); |
| 2518 |
if (sp == NULL) { |
| 2519 |
ret = errno; |
| 2520 |
krb5_warn(context->context, ret, "Unable to keep entries"); |
| 2521 |
krb5_data_free(&entries); |
| 2522 |
return errno; |
| 2523 |
} |
| 2524 |
ret = krb5_store_uint32(sp, 0); |
| 2525 |
if (ret == 0) |
| 2526 |
ret = krb5_store_uint32(sp, now); |
| 2527 |
if (ret == 0) |
| 2528 |
ret = krb5_store_uint32(sp, kadm_nop); /* end of preamble */ |
| 2529 |
if (ret == 0) |
| 2530 |
ret = krb5_store_uint32(sp, LOG_UBER_LEN); /* end of header */ |
| 2531 |
if (ret == 0) |
| 2532 |
ret = krb5_store_uint64(sp, LOG_UBER_SZ); |
| 2533 |
if (ret == 0) |
| 2534 |
ret = krb5_store_uint32(sp, now); |
| 2535 |
if (ret == 0) |
| 2536 |
ret = krb5_store_uint32(sp, last); |
| 2537 |
if (ret == 0) |
| 2538 |
ret = krb5_store_uint32(sp, LOG_UBER_LEN); |
| 2539 |
if (ret == 0) |
| 2540 |
ret = krb5_store_uint32(sp, 0); /* end of trailer */ |
| 2541 |
if (ret == 0) { |
| 2542 |
bytes = krb5_storage_write(sp, entries.data, entries.length); |
| 2543 |
if (bytes == -1) |
| 2544 |
ret = errno; |
| 2545 |
} |
| 2546 |
if (ret == 0) |
| 2547 |
ret = krb5_storage_fsync(sp); |
| 2548 |
/* Confirm all the records now */ |
| 2549 |
if (ret == 0) { |
| 2550 |
if (krb5_storage_seek(sp, LOG_HEADER_SZ, SEEK_SET) == -1) |
| 2551 |
ret = errno; |
| 2552 |
} |
| 2553 |
if (ret == 0) |
| 2554 |
ret = krb5_store_uint64(sp, off); |
| 2555 |
krb5_data_free(&entries); |
| 2556 |
krb5_storage_free(sp); |
| 2557 |
|
| 2558 |
if (ret) { |
| 2559 |
krb5_warn(context->context, ret, "Unable to keep entries"); |
| 2560 |
(void) ftruncate(context->log_context.log_fd, LOG_UBER_SZ); |
| 2561 |
(void) lseek(context->log_context.log_fd, 0, SEEK_SET); |
| 2562 |
return ret; |
| 2563 |
} |
| 2564 |
|
| 2565 |
/* Done. Now rebuild the log_context state. */ |
| 2566 |
(void) lseek(context->log_context.log_fd, off, SEEK_SET); |
| 2567 |
sp = krb5_storage_from_fd(context->log_context.log_fd); |
| 2568 |
if (sp == NULL) |
| 2569 |
return errno ? errno : krb5_enomem(context->context); |
| 2570 |
ret = kadm5_log_goto_end(context, sp); |
| 2571 |
if (ret == 0) { |
| 2572 |
ret = get_version_prev(sp, &context->log_context.version, &last_tstamp); |
| 2573 |
if (ret == 0) |
| 2574 |
context->log_context.last_time = last_tstamp; |
| 2575 |
} |
| 2576 |
krb5_storage_free(sp); |
| 2577 |
return ret; |
| 2578 |
} |
| 2579 |
|
| 2580 |
/* |
| 2581 |
* "Truncate" the log if not read only and over the desired maximum size. We |
| 2582 |
* attempt to retain 1/4 of the existing storage. |
| 2583 |
* |
| 2584 |
* Called after successful log recovery, so at this point we must have no |
| 2585 |
* unconfirmed entries in the log. |
| 2586 |
*/ |
| 2587 |
static kadm5_ret_t |
| 2588 |
truncate_if_needed(kadm5_server_context *context) |
| 2589 |
{ |
| 2590 |
kadm5_ret_t ret = 0; |
| 2591 |
kadm5_log_context *log_context = &context->log_context; |
| 2592 |
size_t maxbytes; |
| 2593 |
struct stat st; |
| 2594 |
|
| 2595 |
if (log_context->log_fd == -1 || log_context->read_only) |
| 2596 |
return 0; |
| 2597 |
if (strcmp(context->log_context.log_file, "/dev/null") == 0) |
| 2598 |
return 0; |
| 2599 |
|
| 2600 |
maxbytes = get_max_log_size(context->context); |
| 2601 |
if (maxbytes <= 0) |
| 2602 |
return 0; |
| 2603 |
|
| 2604 |
if (fstat(log_context->log_fd, &st) == -1) |
| 2605 |
return errno; |
| 2606 |
if (st.st_size == (size_t)st.st_size && (size_t)st.st_size <= maxbytes) |
| 2607 |
return 0; |
| 2608 |
|
| 2609 |
/* Shrink the log by a factor of 4 */ |
| 2610 |
ret = kadm5_log_truncate(context, 0, maxbytes/4); |
| 2611 |
return ret == EINVAL ? 0 : ret; |
| 2612 |
} |
| 2613 |
|
| 2614 |
#ifndef NO_UNIX_SOCKETS |
| 2615 |
|
| 2616 |
static char *default_signal = NULL; |
| 2617 |
static HEIMDAL_MUTEX signal_mutex = HEIMDAL_MUTEX_INITIALIZER; |
| 2618 |
|
| 2619 |
const char * |
| 2620 |
kadm5_log_signal_socket(krb5_context context) |
| 2621 |
{ |
| 2622 |
int ret = 0; |
| 2623 |
|
| 2624 |
HEIMDAL_MUTEX_lock(&signal_mutex); |
| 2625 |
if (!default_signal) |
| 2626 |
ret = asprintf(&default_signal, "%s/signal", hdb_db_dir(context)); |
| 2627 |
if (ret == -1) |
| 2628 |
default_signal = NULL; |
| 2629 |
HEIMDAL_MUTEX_unlock(&signal_mutex); |
| 2630 |
|
| 2631 |
return krb5_config_get_string_default(context, |
| 2632 |
NULL, |
| 2633 |
default_signal, |
| 2634 |
"kdc", |
| 2635 |
"signal_socket", |
| 2636 |
NULL); |
| 2637 |
} |
| 2638 |
|
| 2639 |
#else /* NO_UNIX_SOCKETS */ |
| 2640 |
|
| 2641 |
#define SIGNAL_SOCKET_HOST "127.0.0.1" |
| 2642 |
#define SIGNAL_SOCKET_PORT "12701" |
| 2643 |
|
| 2644 |
kadm5_ret_t |
| 2645 |
kadm5_log_signal_socket_info(krb5_context context, |
| 2646 |
int server_end, |
| 2647 |
struct addrinfo **ret_addrs) |
| 2648 |
{ |
| 2649 |
struct addrinfo hints; |
| 2650 |
struct addrinfo *addrs = NULL; |
| 2651 |
kadm5_ret_t ret = KADM5_FAILURE; |
| 2652 |
int wsret; |
| 2653 |
|
| 2654 |
memset(&hints, 0, sizeof(hints)); |
| 2655 |
|
| 2656 |
hints.ai_flags = AI_NUMERICHOST; |
| 2657 |
if (server_end) |
| 2658 |
hints.ai_flags |= AI_PASSIVE; |
| 2659 |
hints.ai_family = AF_INET; |
| 2660 |
hints.ai_socktype = SOCK_STREAM; |
| 2661 |
hints.ai_protocol = IPPROTO_TCP; |
| 2662 |
|
| 2663 |
wsret = getaddrinfo(SIGNAL_SOCKET_HOST, |
| 2664 |
SIGNAL_SOCKET_PORT, |
| 2665 |
&hints, &addrs); |
| 2666 |
|
| 2667 |
if (wsret != 0) { |
| 2668 |
krb5_set_error_message(context, KADM5_FAILURE, |
| 2669 |
"%s", gai_strerror(wsret)); |
| 2670 |
goto done; |
| 2671 |
} |
| 2672 |
|
| 2673 |
if (addrs == NULL) { |
| 2674 |
krb5_set_error_message(context, KADM5_FAILURE, |
| 2675 |
"getaddrinfo() failed to return address list"); |
| 2676 |
goto done; |
| 2677 |
} |
| 2678 |
|
| 2679 |
*ret_addrs = addrs; |
| 2680 |
addrs = NULL; |
| 2681 |
ret = 0; |
| 2682 |
|
| 2683 |
done: |
| 2684 |
if (addrs) |
| 2685 |
freeaddrinfo(addrs); |
| 2686 |
return ret; |
| 2687 |
} |
| 2688 |
|
| 2689 |
#endif |