Commit | Line | Data |
---|---|---|
f0872424 | 1 | /* $Cambridge: exim/src/src/auths/dovecot.c,v 1.4 2007/01/23 12:22:00 ph10 Exp $ */ |
14aa5a05 PH |
2 | |
3 | /* | |
4 | * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published | |
8 | * by the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include "../exim.h" | |
13 | #include "dovecot.h" | |
14 | ||
15 | #define VERSION_MAJOR 1 | |
16 | #define VERSION_MINOR 0 | |
17 | ||
18 | /* Options specific to the authentication mechanism. */ | |
19 | optionlist auth_dovecot_options[] = { | |
20 | { | |
21 | "server_socket", | |
22 | opt_stringptr, | |
23 | (void *)(offsetof(auth_dovecot_options_block, server_socket)) | |
24 | }, | |
25 | }; | |
26 | ||
27 | /* Size of the options list. An extern variable has to be used so that its | |
28 | address can appear in the tables drtables.c. */ | |
29 | int auth_dovecot_options_count = | |
30 | sizeof(auth_dovecot_options) / sizeof(optionlist); | |
31 | ||
32 | /* Default private options block for the authentication method. */ | |
33 | auth_dovecot_options_block auth_dovecot_option_defaults = { | |
34 | NULL, /* server_socket */ | |
35 | }; | |
36 | ||
37 | /************************************************* | |
38 | * Initialization entry point * | |
39 | *************************************************/ | |
40 | ||
41 | /* Called for each instance, after its options have been read, to | |
42 | enable consistency checks to be done, or anything else that needs | |
43 | to be set up. */ | |
44 | void auth_dovecot_init(auth_instance *ablock) | |
45 | { | |
46 | auth_dovecot_options_block *ob = | |
47 | (auth_dovecot_options_block *)(ablock->options_block); | |
48 | ||
49 | if (ablock->public_name == NULL) | |
50 | ablock->public_name = ablock->name; | |
51 | if (ob->server_socket != NULL) | |
52 | ablock->server = TRUE; | |
53 | ablock->client = FALSE; | |
54 | } | |
55 | ||
56 | static int strcut(char *str, char **ptrs, int nptrs) | |
57 | { | |
58 | char *tmp = str; | |
59 | int n; | |
60 | ||
61 | for (n = 0; n < nptrs; n++) | |
62 | ptrs[n] = NULL; | |
63 | n = 1; | |
64 | ||
65 | while (*str) { | |
66 | if (*str == '\t') { | |
67 | if (n <= nptrs) { | |
68 | *ptrs++ = tmp; | |
69 | tmp = str + 1; | |
70 | *str = 0; | |
71 | } | |
72 | n++; | |
73 | } | |
74 | str++; | |
75 | } | |
76 | ||
77 | if (n < nptrs) | |
78 | *ptrs = tmp; | |
79 | ||
80 | return n; | |
81 | } | |
82 | ||
83 | #define CHECK_COMMAND(str, arg_min, arg_max) do { \ | |
84 | if (strcasecmp((str), args[0]) != 0) \ | |
85 | goto out; \ | |
86 | if (nargs - 1 < (arg_min)) \ | |
87 | goto out; \ | |
88 | if (nargs - 1 > (arg_max)) \ | |
89 | goto out; \ | |
90 | } while (0) | |
91 | ||
92 | #define OUT(msg) do { \ | |
93 | auth_defer_msg = (US msg); \ | |
94 | goto out; \ | |
95 | } while(0) | |
96 | ||
7befa435 PH |
97 | |
98 | ||
14aa5a05 PH |
99 | /************************************************* |
100 | * Server entry point * | |
101 | *************************************************/ | |
102 | ||
103 | int auth_dovecot_server(auth_instance *ablock, uschar *data) | |
104 | { | |
105 | auth_dovecot_options_block *ob = | |
106 | (auth_dovecot_options_block *)(ablock->options_block); | |
107 | struct sockaddr_un sa; | |
108 | char buffer[4096]; | |
109 | char *args[8]; | |
7befa435 PH |
110 | uschar *auth_command; |
111 | uschar *auth_extra_data = US""; | |
14aa5a05 PH |
112 | int nargs, tmp; |
113 | int cuid = 0, cont = 1, found = 0, fd, ret = DEFER; | |
114 | FILE *f; | |
115 | ||
116 | HDEBUG(D_auth) debug_printf("dovecot authentication\n"); | |
117 | ||
118 | memset(&sa, 0, sizeof(sa)); | |
119 | sa.sun_family = AF_UNIX; | |
120 | ||
121 | /* This was the original code here: it is nonsense because strncpy() | |
122 | does not return an integer. I have converted this to use the function | |
123 | that formats and checks length. PH */ | |
124 | ||
125 | /* | |
126 | if (strncpy(sa.sun_path, ob->server_socket, sizeof(sa.sun_path)) < 0) { | |
127 | */ | |
128 | ||
129 | if (!string_format(US sa.sun_path, sizeof(sa.sun_path), "%s", | |
130 | ob->server_socket)) { | |
131 | auth_defer_msg = US"authentication socket path too long"; | |
132 | return DEFER; | |
133 | } | |
134 | ||
135 | auth_defer_msg = US"authentication socket connection error"; | |
136 | ||
137 | fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
138 | if (fd < 0) | |
139 | return DEFER; | |
140 | ||
141 | if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) | |
142 | goto out; | |
143 | ||
144 | f = fdopen(fd, "a+"); | |
145 | if (f == NULL) | |
146 | goto out; | |
147 | ||
148 | auth_defer_msg = US"authentication socket protocol error"; | |
149 | ||
150 | while (cont) { | |
151 | if (fgets(buffer, sizeof(buffer), f) == NULL) | |
152 | OUT("authentication socket read error or premature eof"); | |
153 | ||
154 | buffer[strlen(buffer) - 1] = 0; | |
155 | HDEBUG(D_auth) debug_printf("received: %s\n", buffer); | |
156 | nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0])); | |
157 | ||
158 | switch (toupper(*args[0])) { | |
159 | case 'C': | |
160 | CHECK_COMMAND("CUID", 1, 1); | |
161 | cuid = atoi(args[1]); | |
162 | break; | |
163 | ||
164 | case 'D': | |
165 | CHECK_COMMAND("DONE", 0, 0); | |
166 | cont = 0; | |
167 | break; | |
168 | ||
169 | case 'M': | |
170 | CHECK_COMMAND("MECH", 1, INT_MAX); | |
171 | if (strcmpic(US args[1], ablock->public_name) == 0) | |
172 | found = 1; | |
173 | break; | |
174 | ||
175 | case 'S': | |
176 | CHECK_COMMAND("SPID", 1, 1); | |
177 | break; | |
178 | ||
179 | case 'V': | |
180 | CHECK_COMMAND("VERSION", 2, 2); | |
181 | if (atoi(args[1]) != VERSION_MAJOR) | |
182 | OUT("authentication socket protocol version mismatch"); | |
183 | break; | |
184 | ||
185 | default: | |
186 | goto out; | |
187 | } | |
188 | } | |
189 | ||
190 | if (!found) | |
191 | goto out; | |
192 | ||
7befa435 PH |
193 | /* Added by PH: data must not contain tab (as it is |
194 | b64 it shouldn't, but check for safety). */ | |
195 | ||
196 | if (Ustrchr(data, '\t') != NULL) { | |
197 | ret = FAIL; | |
198 | goto out; | |
199 | } | |
200 | ||
201 | /* Added by PH: extra fields when TLS is in use or if the TCP/IP | |
202 | connection is local. */ | |
203 | ||
204 | if (tls_cipher != NULL) | |
205 | auth_extra_data = string_sprintf("secured\t%s%s", | |
206 | tls_certificate_verified? "valid-client-cert" : "", | |
207 | tls_certificate_verified? "\t" : ""); | |
208 | else if (Ustrcmp(sender_host_address, interface_address) == 0) | |
209 | auth_extra_data = US"secured\t"; | |
210 | ||
14aa5a05 PH |
211 | |
212 | /**************************************************************************** | |
213 | The code below was the original code here. It didn't work. A reading of the | |
214 | file auth-protocol.txt.gz that came with Dovecot 1.0_beta8 indicated that | |
7befa435 PH |
215 | this was not right. Maybe something changed. I changed it to move the |
216 | service indication into the AUTH command, and it seems to be better. PH | |
14aa5a05 PH |
217 | |
218 | fprintf(f, "VERSION\t%d\t%d\r\nSERVICE\tSMTP\r\nCPID\t%d\r\n" | |
219 | "AUTH\t%d\t%s\trip=%s\tlip=%s\tresp=%s\r\n", | |
220 | VERSION_MAJOR, VERSION_MINOR, getpid(), cuid, | |
221 | ablock->public_name, sender_host_address, interface_address, | |
222 | data ? (char *) data : ""); | |
7befa435 PH |
223 | |
224 | Subsequently, the command was modified to add "secured" and "valid-client- | |
225 | cert" when relevant. | |
14aa5a05 PH |
226 | ****************************************************************************/ |
227 | ||
7befa435 PH |
228 | auth_command = string_sprintf("VERSION\t%d\t%d\nCPID\t%d\n" |
229 | "AUTH\t%d\t%s\tservice=smtp\t%srip=%s\tlip=%s\tresp=%s\n", | |
14aa5a05 | 230 | VERSION_MAJOR, VERSION_MINOR, getpid(), cuid, |
7befa435 PH |
231 | ablock->public_name, auth_extra_data, sender_host_address, |
232 | interface_address, data ? (char *) data : ""); | |
14aa5a05 | 233 | |
7befa435 PH |
234 | fprintf(f, "%s", auth_command); |
235 | HDEBUG(D_auth) debug_printf("sent: %s", auth_command); | |
14aa5a05 PH |
236 | |
237 | while (1) { | |
238 | if (fgets(buffer, sizeof(buffer), f) == NULL) { | |
239 | auth_defer_msg = US"authentication socket read error or premature eof"; | |
240 | goto out; | |
241 | } | |
242 | ||
243 | buffer[strlen(buffer) - 1] = 0; | |
244 | HDEBUG(D_auth) debug_printf("received: %s\n", buffer); | |
245 | nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0])); | |
246 | ||
247 | if (atoi(args[1]) != cuid) | |
248 | OUT("authentication socket connection id mismatch"); | |
249 | ||
250 | switch (toupper(*args[0])) { | |
251 | case 'C': | |
252 | CHECK_COMMAND("CONT", 1, 2); | |
253 | ||
254 | tmp = auth_get_no64_data(&data, US args[2]); | |
255 | if (tmp != OK) { | |
256 | ret = tmp; | |
257 | goto out; | |
258 | } | |
259 | ||
7befa435 PH |
260 | /* Added by PH: data must not contain tab (as it is |
261 | b64 it shouldn't, but check for safety). */ | |
262 | ||
263 | if (Ustrchr(data, '\t') != NULL) { | |
264 | ret = FAIL; | |
265 | goto out; | |
266 | } | |
267 | ||
14aa5a05 PH |
268 | if (fprintf(f, "CONT\t%d\t%s\r\n", cuid, data) < 0) |
269 | OUT("authentication socket write error"); | |
270 | ||
271 | break; | |
272 | ||
273 | case 'F': | |
274 | CHECK_COMMAND("FAIL", 1, 2); | |
275 | ||
276 | /* FIXME: add proper response handling */ | |
277 | if (args[2]) { | |
278 | uschar *p = US strchr(args[2], '='); | |
279 | if (p) { | |
280 | ++p; | |
f0872424 PH |
281 | expand_nstring[1] = auth_vars[0] = |
282 | string_copy(p); /* PH */ | |
14aa5a05 PH |
283 | expand_nlength[1] = Ustrlen(p); |
284 | expand_nmax = 1; | |
285 | } | |
286 | } | |
287 | ||
288 | ret = FAIL; | |
289 | goto out; | |
290 | ||
291 | case 'O': | |
292 | CHECK_COMMAND("OK", 2, 2); | |
293 | { | |
294 | /* FIXME: add proper response handling */ | |
295 | uschar *p = US strchr(args[2], '='); | |
296 | if (!p) | |
297 | OUT("authentication socket protocol error, username missing"); | |
298 | ||
299 | p++; | |
f0872424 PH |
300 | expand_nstring[1] = auth_vars[0] = |
301 | string_copy(p); /* PH */ | |
14aa5a05 PH |
302 | expand_nlength[1] = Ustrlen(p); |
303 | expand_nmax = 1; | |
304 | } | |
305 | ret = OK; | |
306 | /* fallthrough */ | |
307 | ||
308 | default: | |
309 | goto out; | |
310 | } | |
311 | } | |
312 | ||
313 | out: close(fd); | |
16ff981e PH |
314 | |
315 | /* Expand server_condition as an authorization check */ | |
316 | return (ret == OK)? auth_check_serv_cond(ablock) : ret; | |
14aa5a05 | 317 | } |