static void apply_entry(kadm5_server_context *server_context, uint32_t ver, time_t timestamp, enum kadm_ops op, uint32_t len, krb5_storage *sp, void *ctx) { struct replay_options *opt = ctx; krb5_error_code ret; if((opt->start_version_integer != -1 && ver < (uint32_t)opt->start_version_integer) || (opt->end_version_integer != -1 && ver > (uint32_t)opt->end_version_integer)) { /* XXX skip this entry */ krb5_storage_seek(sp, len, SEEK_CUR); return; } printf ("ver %u... ", ver); fflush (stdout); ret = kadm5_log_replay (server_context, op, ver, len, sp); if (ret) krb5_warn (server_context->context, ret, "kadm5_log_replay"); printf ("done\n"); }
static void receive_loop (krb5_context context, krb5_storage *sp, kadm5_server_context *server_context) { int ret; off_t left, right; void *buf; int32_t vers, vers2; ssize_t sret; /* * Seek to the current version of the local database. */ do { int32_t len, timestamp, tmp; enum kadm_ops op; if(krb5_ret_int32 (sp, &vers) != 0) return; krb5_ret_int32 (sp, ×tamp); krb5_ret_int32 (sp, &tmp); op = tmp; krb5_ret_int32 (sp, &len); if (vers <= server_context->log_context.version) krb5_storage_seek(sp, len + 8, SEEK_CUR); } while(vers <= server_context->log_context.version); /* * Read up rest of the entires into the memory... */ left = krb5_storage_seek (sp, -16, SEEK_CUR); right = krb5_storage_seek (sp, 0, SEEK_END); buf = malloc (right - left); if (buf == NULL && (right - left) != 0) krb5_errx (context, 1, "malloc: no memory"); /* * ...and then write them out to the on-disk log. */ krb5_storage_seek (sp, left, SEEK_SET); krb5_storage_read (sp, buf, right - left); sret = write (server_context->log_context.log_fd, buf, right-left); if (sret != right - left) krb5_err(context, 1, errno, "Failed to write log to disk"); ret = fsync (server_context->log_context.log_fd); if (ret) krb5_err(context, 1, errno, "Failed to sync log to disk"); free (buf); /* * Go back to the startpoint and start to commit the entires to * the database. */ krb5_storage_seek (sp, left, SEEK_SET); for(;;) { int32_t len, len2, timestamp, tmp; off_t cur, cur2; enum kadm_ops op; if(krb5_ret_int32 (sp, &vers) != 0) break; ret = krb5_ret_int32 (sp, ×tamp); if (ret) krb5_errx(context, 1, "entry %ld: too short", (long)vers); ret = krb5_ret_int32 (sp, &tmp); if (ret) krb5_errx(context, 1, "entry %ld: too short", (long)vers); op = tmp; ret = krb5_ret_int32 (sp, &len); if (ret) krb5_errx(context, 1, "entry %ld: too short", (long)vers); if (len < 0) krb5_errx(context, 1, "log is corrupted, " "negative length of entry version %ld: %ld", (long)vers, (long)len); cur = krb5_storage_seek(sp, 0, SEEK_CUR); krb5_warnx (context, "replaying entry %d", (int)vers); ret = kadm5_log_replay (server_context, op, vers, len, sp); if (ret) { const char *s = krb5_get_error_message(server_context->context, ret); krb5_warnx (context, "kadm5_log_replay: %ld. Lost entry entry, " "Database out of sync ?: %s (%d)", (long)vers, s ? s : "unknown error", ret); krb5_free_error_message(context, s); } { /* * Make sure the krb5_log_replay does the right thing wrt * reading out data from the sp. */ cur2 = krb5_storage_seek(sp, 0, SEEK_CUR); if (cur + len != cur2) krb5_errx(context, 1, "kadm5_log_reply version: %ld didn't read the whole entry", (long)vers); } if (krb5_ret_int32 (sp, &len2) != 0) krb5_errx(context, 1, "entry %ld: postamble too short", (long)vers); if(krb5_ret_int32 (sp, &vers2) != 0) krb5_errx(context, 1, "entry %ld: postamble too short", (long)vers); if (len != len2) krb5_errx(context, 1, "entry %ld: len != len2", (long)vers); if (vers != vers2) krb5_errx(context, 1, "entry %ld: vers != vers2", (long)vers); } /* * Update version */ server_context->log_context.version = vers; }