bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainendump_timestamp(struct istream *input, const char *name, const char *value)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen t = hex2dec((const void *)value, strlen(value));
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if (t == 0) {
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen printf("%s = %ld (%s)\n", name, (long)t, unixdate2str(t));
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainendump_size(struct istream *input, const char *name, const char *value)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen size = hex2dec((const void *)value, strlen(value));
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainenstatic unsigned int dump_file_hdr(struct istream *input)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if ((line = i_stream_read_next_line(input)) == NULL)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen /* check version */
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if (version == NULL || !str_is_numeric(version, ' '))
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("%s is not a dbox file", i_stream_get_name(input));
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("Unsupported dbox file version %s", version);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen switch (**arg) {
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen msg_hdr_size = hex2dec((const void *)(*arg + 1),
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen printf("file.msg_header_size = %u\n", msg_hdr_size);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen dump_timestamp(input, "file.create_stamp", *arg + 1);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen printf("file.unknown-%c = %s\n", **arg, *arg + 1);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("Missing msg_header_size in file header");
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainendump_msg_hdr(struct istream *input, unsigned int hdr_size, uoff_t *msg_size_r)
534e41e436a7546f36f61e0dc49c0c191d850f6bPhil Carmody if (i_stream_read_bytes(input, &data, &size, hdr_size) <= 0) {
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("Partial message header read at %"PRIuUOFF_T": "
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen "%"PRIuSIZE_T" bytes", input->v_offset, size);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen printf("offset %"PRIuUOFF_T":\n", input->v_offset);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("file.hdr_size too small: %u", hdr_size);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if (memcmp(hdr.magic_pre, DBOX_MAGIC_PRE, sizeof(hdr.magic_pre)) != 0)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("dbox wrong pre-magic at %"PRIuUOFF_T, input->v_offset);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen t_strndup(hdr.message_size_hex, sizeof(hdr.message_size_hex)));
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainenstatic void dump_msg_metadata(struct istream *input)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen /* verify magic */
534e41e436a7546f36f61e0dc49c0c191d850f6bPhil Carmody if (i_stream_read_bytes(input, &data, &size, sizeof(hdr)) <= 0) {
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("dbox missing metadata at %"PRIuUOFF_T,
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if (memcmp(hdr.magic_post, DBOX_MAGIC_POST, sizeof(hdr.magic_post)) != 0)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("dbox wrong post-magic at %"PRIuUOFF_T, input->v_offset);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen /* dump the metadata */
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if ((line = i_stream_read_next_line(input)) == NULL)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen i_fatal("dbox metadata ended unexpectedly at EOF");
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen dump_timestamp(input, "msg.received", line + 1);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)dump_size(input, "msg.physical-size", line + 1);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)dump_size(input, "msg.virtual-size", line + 1);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen printf("msg.obsolete-%c = %s\n", *line, line + 1);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainenstatic bool dump_msg(struct istream *input, unsigned int hdr_size)
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen if (!dump_msg_hdr(input, hdr_size, &msg_size))
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainenstatic void cmd_dump_dbox(int argc ATTR_UNUSED, char *argv[])
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen input = i_stream_create_fd_autoclose(&fd, (size_t)-1);
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen const char *p;
f05f569c2ee0d1f5216614856ff0ae7bd60f27faTimo Sirainen return strncmp(p, "m.", 2) == 0 || strncmp(p, "u.", 2) == 0;