0
|
1 |
/* |
|
2 |
* Copyright (c) 2014 Sunil Nimmagadda <sunil@nimmagadda.net> |
|
3 |
* |
|
4 |
* Permission to use, copy, modify, and distribute this software for any |
|
5 |
* purpose with or without fee is hereby granted, provided that the above |
|
6 |
* copyright notice and this permission notice appear in all copies. |
|
7 |
* |
|
8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
15 |
*/ |
|
16 |
|
|
17 |
#include <sys/tree.h> |
|
18 |
|
|
19 |
#include <sha1.h> |
|
20 |
|
|
21 |
#include "imsgev.h" |
|
22 |
#include "iobuf.h" |
|
23 |
#include "ioev.h" |
|
24 |
|
|
25 |
#define ARGLEN 40 |
|
26 |
#define POP3S 0x01 |
|
27 |
#define F_DELE 0x01 |
|
28 |
|
|
29 |
struct passwd; |
|
30 |
|
|
31 |
enum imsg_type { |
|
32 |
IMSG_AUTH, |
|
33 |
IMSG_MAILDROP_INIT, |
|
34 |
IMSG_MAILDROP_RETR, |
|
35 |
IMSG_MAILDROP_DELE, |
|
36 |
IMSG_MAILDROP_RSET, |
|
37 |
IMSG_MAILDROP_LIST, |
|
38 |
IMSG_MAILDROP_LISTALL, |
|
39 |
IMSG_MAILDROP_UPDATE |
|
40 |
}; |
|
41 |
|
|
42 |
enum m_type { |
|
43 |
M_MBOX, |
|
44 |
M_MAILDIR |
|
45 |
}; |
|
46 |
|
|
47 |
struct msg { |
|
48 |
union { |
|
49 |
SIMPLEQ_ENTRY(msg) q_entry; |
|
50 |
RB_ENTRY(msg) t_entry; |
|
51 |
} e; |
|
52 |
char hash[SHA1_DIGEST_STRING_LENGTH]; |
|
53 |
size_t sz; |
|
54 |
size_t nlines; |
|
55 |
union { |
|
56 |
long offset; |
|
57 |
const char *fname; |
|
58 |
} u; |
|
59 |
int flags; |
|
60 |
}; |
|
61 |
|
|
62 |
struct mdrop { |
|
63 |
union { |
|
64 |
SIMPLEQ_HEAD(, msg) q_msgs; |
|
65 |
RB_HEAD(msgtree, msg) t_msgs; |
|
66 |
} e; |
|
67 |
size_t nmsgs; |
|
68 |
size_t sz; |
|
69 |
struct msg **msgs_index; /* random access to msgs */ |
|
70 |
int fd; |
|
71 |
}; |
|
72 |
|
|
73 |
struct stats { |
|
74 |
size_t nmsgs; |
|
75 |
size_t sz; |
|
76 |
}; |
|
77 |
|
|
78 |
struct retr_req { |
|
79 |
unsigned int idx; |
|
80 |
unsigned int ntop; |
|
81 |
int top; |
|
82 |
}; |
|
83 |
|
|
84 |
struct retr_res { |
|
85 |
size_t nlines; |
|
86 |
long offset; |
|
87 |
unsigned int ntop; |
|
88 |
int top; |
|
89 |
}; |
|
90 |
|
|
91 |
struct list_req { |
|
92 |
unsigned int idx; |
|
93 |
int uidl; |
|
94 |
}; |
|
95 |
|
|
96 |
struct list_res { |
|
97 |
unsigned int idx; |
|
98 |
union { |
|
99 |
size_t sz; |
|
100 |
char hash[SHA1_DIGEST_STRING_LENGTH]; |
|
101 |
} u; |
|
102 |
int uidl; |
|
103 |
}; |
|
104 |
|
|
105 |
struct m_backend { |
|
106 |
int (*init)(struct mdrop *, size_t *, size_t *); |
|
107 |
int (*retr)(struct mdrop *, unsigned int, size_t *, size_t *); |
|
108 |
int (*update)(struct mdrop *); |
|
109 |
}; |
|
110 |
|
|
111 |
struct auth_req { |
|
112 |
char user[ARGLEN]; |
|
113 |
char pass[ARGLEN]; |
|
114 |
}; |
|
115 |
|
|
116 |
struct listener { |
|
117 |
struct sockaddr_storage ss; |
|
118 |
struct event ev; |
|
119 |
struct event pause; |
|
120 |
int flags; |
|
121 |
int sock; |
|
122 |
}; |
|
123 |
|
|
124 |
enum state { |
|
125 |
AUTH, |
|
126 |
TRANSACTION, |
|
127 |
UPDATE |
|
128 |
}; |
|
129 |
|
|
130 |
struct session { |
|
131 |
SPLAY_ENTRY(session) entry; |
|
132 |
struct imsgev iev_maildrop; |
|
133 |
struct iobuf iobuf; |
|
134 |
struct io io; |
|
135 |
char user[ARGLEN]; |
|
136 |
char pass[ARGLEN]; |
|
137 |
size_t m_sz; |
|
138 |
size_t nmsgs; |
|
139 |
struct listener *l; |
|
140 |
uint32_t id; |
|
141 |
int sock; |
|
142 |
int flags; |
|
143 |
enum state state; |
|
144 |
}; |
|
145 |
|
|
146 |
/* pop3e.c */ |
|
147 |
pid_t pop3_main(int [2], struct passwd *); |
|
148 |
|
|
149 |
/* session.c */ |
|
150 |
void session_init(struct listener *, int); |
|
151 |
void session_close(struct session *, int); |
|
152 |
void session_reply(struct session *, char *, ...); |
|
153 |
void session_set_state(struct session *, enum state); |
|
154 |
void session_imsgev_init(struct session *, int); |
|
155 |
SPLAY_HEAD(session_tree, session); |
|
156 |
int session_cmp(struct session *, struct session *); |
|
157 |
SPLAY_PROTOTYPE(session_tree, session, entry, session_cmp); |
|
158 |
|
|
159 |
/* maildrop.c */ |
|
160 |
void maildrop_init(uint32_t, int [2], struct passwd *, |
|
161 |
int, const char *); |
|
162 |
|
|
163 |
/* util.c */ |
|
164 |
void set_nonblocking(int); |
|
165 |
void log_init(int); |
|
166 |
void logit(int, const char *, ...); |
|
167 |
void vlog(int, const char *, va_list); |
|
168 |
void fatal(const char *); |
|
169 |
void fatalx(const char *); |
|
170 |
void *xcalloc(size_t, size_t, const char *); |
|
171 |
void iobuf_xfqueue(struct iobuf *, const char *, const char *, ...); |
|
172 |
void iobuf_xqueue(struct iobuf *, const char *, const void *, size_t); |
|
173 |
int imsgev_xcompose(struct imsgev *, u_int16_t, u_int32_t, |
|
174 |
uint32_t, int, void *, u_int16_t, const char *); |
|
175 |
int get_index(struct session *, const char *, unsigned int *); |
|
176 |
void log_connect(uint32_t, struct sockaddr_storage *, socklen_t); |