summaryrefslogtreecommitdiff
path: root/usr.sbin/popa3d/pop_trans.c
blob: 05ecbec76a3a656a26320158249842e491ba64be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* $OpenBSD: pop_trans.c,v 1.1 2001/08/19 13:05:57 deraadt Exp $ */

/*
 * TRANSACTION state handling.
 */

#include <stdio.h>
#include <syslog.h>

#include "params.h"
#include "protocol.h"
#include "database.h"
#include "mailbox.h"

static int pop_trans_quit(char *params)
{
	if (params) return POP_ERROR;
	return POP_STATE;
}

static int pop_trans_noop(char *params)
{
	if (params) return POP_ERROR;
	return POP_OK;
}

static int pop_trans_stat(char *params)
{
	if (params) return POP_ERROR;
	if (pop_reply("+OK %d %ld", db.visible_count, db.visible_size))
		return POP_CRASH;
	return POP_QUIET;
}

static int pop_trans_list_or_uidl(char *params, int uidl)
{
	int number;
	struct db_message *msg;

	if (params) {
		number = pop_get_int(&params);
		if (number < 1 || number > db.total_count || params)
			return POP_ERROR;
		msg = db.array[number - 1];
		if (msg->flags & MSG_DELETED) return POP_ERROR;
		if (uidl) {
			if (pop_reply("+OK %d "
			    "%02x%02x%02x%02x%02x%02x%02x%02x",
			    number,
			    msg->hash[3], msg->hash[2],
			    msg->hash[1], msg->hash[0],
			    msg->hash[7], msg->hash[6],
			    msg->hash[5], msg->hash[4]))
				return POP_CRASH;
		} else
			if (pop_reply("+OK %d %ld", number, msg->size))
				return POP_CRASH;
		return POP_QUIET;
	}

	if (pop_reply_ok()) return POP_CRASH;
	for (number = 1; number <= db.total_count; number++) {
		msg = db.array[number - 1];
		if (msg->flags & MSG_DELETED) continue;
		if (uidl) {
			if (pop_reply("%d "
			    "%02x%02x%02x%02x%02x%02x%02x%02x",
			    number,
			    msg->hash[3], msg->hash[2],
			    msg->hash[1], msg->hash[0],
			    msg->hash[7], msg->hash[6],
			    msg->hash[5], msg->hash[4]))
				return POP_CRASH;
		} else
			if (pop_reply("%d %ld", number, msg->size))
				return POP_CRASH;
	}
	if (pop_reply_terminate()) return POP_CRASH;

	return POP_QUIET;
}

static int pop_trans_list(char *params)
{
	return pop_trans_list_or_uidl(params, 0);
}

static int pop_trans_uidl(char *params)
{
	return pop_trans_list_or_uidl(params, 1);
}

static int pop_trans_retr(char *params)
{
	int number;
	struct db_message *msg;

	number = pop_get_int(&params);
	if (number < 1 || number > db.total_count || params) return POP_ERROR;
	msg = db.array[number - 1];
	if (msg->flags & MSG_DELETED) return POP_ERROR;
	if (mailbox_get(msg, -1)) return POP_CRASH;
#if POP_SUPPORT_LAST
	if (number > db.last) db.last = number;
#endif
	return POP_QUIET;
}

static int pop_trans_top(char *params)
{
	int number, lines;
	struct db_message *msg;

	number = pop_get_int(&params);
	if (number < 1 || number > db.total_count) return POP_ERROR;
	lines = pop_get_int(&params);
	if (lines < 0 || params) return POP_ERROR;
	msg = db.array[number - 1];
	if (msg->flags & MSG_DELETED) return POP_ERROR;
	if (mailbox_get(msg, lines)) return POP_CRASH;
	return POP_QUIET;
}

static int pop_trans_dele(char *params)
{
	int number;
	struct db_message *msg;

	number = pop_get_int(&params);
	if (number < 1 || number > db.total_count || params) return POP_ERROR;
	msg = db.array[number - 1];
	if (db_delete(msg)) return POP_ERROR;
#if POP_SUPPORT_LAST
	if (number > db.last) db.last = number;
#endif
	return POP_OK;
}

static int pop_trans_rset(char *params)
{
	struct db_message *msg;

	if (params) return POP_ERROR;

	if ((msg = db.head))
	do {
		msg->flags &= ~MSG_DELETED;
	} while ((msg = msg->next));

	db.visible_count = db.total_count;
	db.visible_size = db.total_size;
	db.flags &= ~DB_DIRTY;
#if POP_SUPPORT_LAST
	db.last = 0;
#endif

	return POP_OK;
}

#if POP_SUPPORT_LAST
static int pop_trans_last(char *params)
{
	if (params) return POP_ERROR;
	if (pop_reply("+OK %d", db.last)) return POP_CRASH;
	return POP_QUIET;
}
#endif

static struct pop_command pop_trans_commands[] = {
	{"QUIT", pop_trans_quit},
	{"NOOP", pop_trans_noop},
	{"STAT", pop_trans_stat},
	{"LIST", pop_trans_list},
	{"UIDL", pop_trans_uidl},
	{"RETR", pop_trans_retr},
	{"TOP", pop_trans_top},
	{"DELE", pop_trans_dele},
	{"RSET", pop_trans_rset},
#if POP_SUPPORT_LAST
	{"LAST", pop_trans_last},
#endif
	{NULL}
};

static int db_load(char *spool, char *mailbox)
{
	db_init();

	if (mailbox_open(spool, mailbox)) return 1;

	if (db_fix()) {
		mailbox_close();
		return 1;
	}

	return 0;
}

int do_pop_trans(char *spool, char *mailbox)
{
	int result;

	if (!pop_sane()) return 1;

	if (db_load(spool, mailbox)) {
		syslog(SYSLOG_PRIORITY, "Failed to open mailbox");
		pop_reply_error();
		return 0;
	}

	syslog(SYSLOG_PRIORITY, "%d message%s (%ld byte%s) loaded",
		db.total_count, db.total_count == 1 ? "" : "s",
		db.total_size, db.total_size == 1 ? "" : "s");

	if (pop_reply_ok())
		result = POP_CRASH;
	else
	switch ((result = pop_handle_state(pop_trans_commands))) {
	case POP_STATE:
		if (mailbox_update()) {
			if (db.flags & DB_STALE) break;
			syslog(SYSLOG_PRIORITY, "Failed to update mailbox");
			pop_reply_error();
			break;
		}

		syslog(SYSLOG_PRIORITY, "%d (%ld) deleted, %d (%ld) left",
			db.total_count - db.visible_count,
			db.total_size - db.visible_size,
			db.visible_count,
			db.visible_size);
		pop_reply_ok();
		break;

	case POP_TIMED_OUT:
		syslog(SYSLOG_PRIORITY, "Connection timed out");
	}

	if (db.flags & DB_STALE)
		syslog(SYSLOG_PRIORITY, "Another MUA active, giving up");
	else
	if (result == POP_CRASH)
		syslog(SYSLOG_PRIORITY, "Session crashed");

	mailbox_close();

	return 0;
}