@ -123,9 +123,6 @@ static int nfvasprintf(char **strp, const char *fmt, va_list ap)
return len;
return len;
}
}
static void arc4_init(void);
static unsigned char arc4_getbyte(void);
struct imap_server_conf {
struct imap_server_conf {
char *name;
char *name;
char *tunnel;
char *tunnel;
@ -489,52 +486,6 @@ static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
return ret;
return ret;
}
}
static struct {
unsigned char i, j, s[256];
} rs;
static void arc4_init(void)
{
int i, fd;
unsigned char j, si, dat[128];
if ((fd = open("/dev/urandom", O_RDONLY)) < 0 && (fd = open("/dev/random", O_RDONLY)) < 0) {
fprintf(stderr, "Fatal: no random number source available.\n");
exit(3);
}
if (read_in_full(fd, dat, 128) != 128) {
fprintf(stderr, "Fatal: cannot read random number source.\n");
exit(3);
}
close(fd);
for (i = 0; i < 256; i++)
rs.s[i] = i;
for (i = j = 0; i < 256; i++) {
si = rs.s[i];
j += si + dat[i & 127];
rs.s[i] = rs.s[j];
rs.s[j] = si;
}
rs.i = rs.j = 0;
for (i = 0; i < 256; i++)
arc4_getbyte();
}
static unsigned char arc4_getbyte(void)
{
unsigned char si, sj;
rs.i++;
si = rs.s[rs.i];
rs.j += si;
sj = rs.s[rs.j];
rs.s[rs.i] = sj;
rs.s[rs.j] = si;
return rs.s[(si + sj) & 0xff];
}
static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
struct imap_cmd_cb *cb,
struct imap_cmd_cb *cb,
const char *fmt, va_list ap)
const char *fmt, va_list ap)
@ -1198,88 +1149,20 @@ static int imap_make_flags(int flags, char *buf)
return d;
return d;
}
}
#define TUIDL 8
static int imap_store_msg(struct store *gctx, struct msg_data *data)
static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid)
{
{
struct imap_store *ctx = (struct imap_store *)gctx;
struct imap_store *ctx = (struct imap_store *)gctx;
struct imap *imap = ctx->imap;
struct imap *imap = ctx->imap;
struct imap_cmd_cb cb;
struct imap_cmd_cb cb;
char *fmap, *buf;
const char *prefix, *box;
const char *prefix, *box;
int ret, i, j, d, len, extra, nocr;
int ret, d;
int start, sbreak = 0, ebreak = 0;
char flagstr[128];
char flagstr[128], tuid[TUIDL * 2 + 1];
memset(&cb, 0, sizeof(cb));
memset(&cb, 0, sizeof(cb));
fmap = data->data;
cb.dlen = data->len;
len = data->len;
cb.data = xmalloc(cb.dlen);
nocr = !data->crlf;
memcpy(cb.data, data->data, data->len);
extra = 0, i = 0;
if (!CAP(UIDPLUS) && uid) {
nloop:
start = i;
while (i < len)
if (fmap[i++] == '\n') {
extra += nocr;
if (i - 2 + nocr == start) {
sbreak = ebreak = i - 2 + nocr;
goto mktid;
}
if (!memcmp(fmap + start, "X-TUID: ", 8)) {
extra -= (ebreak = i) - (sbreak = start) + nocr;
goto mktid;
}
goto nloop;
}
/* invalid message */
free(fmap);
return DRV_MSG_BAD;
mktid:
for (j = 0; j < TUIDL; j++)
sprintf(tuid + j * 2, "%02x", arc4_getbyte());
extra += 8 + TUIDL * 2 + 2;
}
if (nocr)
for (; i < len; i++)
if (fmap[i] == '\n')
extra++;
cb.dlen = len + extra;
buf = cb.data = xmalloc(cb.dlen);
i = 0;
if (!CAP(UIDPLUS) && uid) {
if (nocr) {
for (; i < sbreak; i++)
if (fmap[i] == '\n') {
*buf++ = '\r';
*buf++ = '\n';
} else
*buf++ = fmap[i];
} else {
memcpy(buf, fmap, sbreak);
buf += sbreak;
}
memcpy(buf, "X-TUID: ", 8);
buf += 8;
memcpy(buf, tuid, TUIDL * 2);
buf += TUIDL * 2;
*buf++ = '\r';
*buf++ = '\n';
i = ebreak;
}
if (nocr) {
for (; i < len; i++)
if (fmap[i] == '\n') {
*buf++ = '\r';
*buf++ = '\n';
} else
*buf++ = fmap[i];
} else
memcpy(buf, fmap + i, len - i);
free(fmap);
d = 0;
d = 0;
if (data->flags) {
if (data->flags) {
@ -1288,25 +1171,13 @@ static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid)
}
}
flagstr[d] = 0;
flagstr[d] = 0;
if (!uid) {
box = gctx->conf->trash;
prefix = ctx->prefix;
cb.create = 1;
if (ctx->trashnc)
imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
} else {
box = gctx->name;
box = gctx->name;
prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
cb.create = 0;
cb.create = 0;
}
cb.ctx = uid;
ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
imap->caps = imap->rcaps;
imap->caps = imap->rcaps;
if (ret != DRV_OK)
if (ret != DRV_OK)
return ret;
return ret;
if (!uid)
ctx->trashnc = 0;
else
gctx->count++;
gctx->count++;
return DRV_OK;
return DRV_OK;
@ -1483,7 +1354,6 @@ int main(int argc, char **argv)
{
{
struct msg_data all_msgs, msg;
struct msg_data all_msgs, msg;
struct store *ctx = NULL;
struct store *ctx = NULL;
int uid = 0;
int ofs = 0;
int ofs = 0;
int r;
int r;
int total, n = 0;
int total, n = 0;
@ -1491,9 +1361,6 @@ int main(int argc, char **argv)
git_extract_argv0_path(argv[0]);
git_extract_argv0_path(argv[0]);
/* init the random number generator */
arc4_init();
setup_git_directory_gently(&nongit_ok);
setup_git_directory_gently(&nongit_ok);
git_config(git_imap_config, NULL);
git_config(git_imap_config, NULL);
@ -1540,7 +1407,7 @@ int main(int argc, char **argv)
break;
break;
if (server.use_html)
if (server.use_html)
wrap_in_html(&msg);
wrap_in_html(&msg);
r = imap_store_msg(ctx, &msg, &uid);
r = imap_store_msg(ctx, &msg);
if (r != DRV_OK)
if (r != DRV_OK)
break;
break;
n++;
n++;