Change dovecot authenticator to use read/write instead of fgets/fprint
[exim.git] / src / src / auths / dovecot.c
CommitLineData
57c2c631 1/* $Cambridge: exim/src/src/auths/dovecot.c,v 1.7 2007/03/01 14:06:56 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
57c2c631
PH
12/* A number of modifications have been made to the original code. Originally I
13commented them specially, but now they are getting quite extensive, so I have
14ceased doing that. The biggest change is to use unbuffered I/O on the socket
15because using C buffered I/O gives problems on some operating systems. PH */
16
14aa5a05
PH
17#include "../exim.h"
18#include "dovecot.h"
19
20#define VERSION_MAJOR 1
21#define VERSION_MINOR 0
22
23/* Options specific to the authentication mechanism. */
24optionlist auth_dovecot_options[] = {
25 {
57c2c631
PH
26 "server_socket",
27 opt_stringptr,
28 (void *)(offsetof(auth_dovecot_options_block, server_socket))
14aa5a05
PH
29 },
30};
31
32/* Size of the options list. An extern variable has to be used so that its
33address can appear in the tables drtables.c. */
57c2c631 34
14aa5a05
PH
35int auth_dovecot_options_count =
36 sizeof(auth_dovecot_options) / sizeof(optionlist);
37
38/* Default private options block for the authentication method. */
57c2c631 39
14aa5a05
PH
40auth_dovecot_options_block auth_dovecot_option_defaults = {
41 NULL, /* server_socket */
42};
43
57c2c631
PH
44
45/* Static variables for reading from the socket */
46
47static uschar sbuffer[256];
48static int sbp;
49
50
51
14aa5a05
PH
52/*************************************************
53 * Initialization entry point *
54 *************************************************/
55
56/* Called for each instance, after its options have been read, to
57enable consistency checks to be done, or anything else that needs
58to be set up. */
57c2c631 59
14aa5a05
PH
60void auth_dovecot_init(auth_instance *ablock)
61{
62 auth_dovecot_options_block *ob =
63 (auth_dovecot_options_block *)(ablock->options_block);
64
65 if (ablock->public_name == NULL)
66 ablock->public_name = ablock->name;
67 if (ob->server_socket != NULL)
68 ablock->server = TRUE;
69 ablock->client = FALSE;
70}
71
57c2c631 72static int strcut(uschar *str, uschar **ptrs, int nptrs)
14aa5a05 73{
57c2c631 74 uschar *tmp = str;
14aa5a05
PH
75 int n;
76
77 for (n = 0; n < nptrs; n++)
78 ptrs[n] = NULL;
79 n = 1;
80
81 while (*str) {
82 if (*str == '\t') {
83 if (n <= nptrs) {
84 *ptrs++ = tmp;
85 tmp = str + 1;
86 *str = 0;
87 }
88 n++;
89 }
90 str++;
91 }
92
93 if (n < nptrs)
94 *ptrs = tmp;
95
96 return n;
97}
98
99#define CHECK_COMMAND(str, arg_min, arg_max) do { \
57c2c631 100 if (strcmpic(US(str), args[0]) != 0) \
14aa5a05
PH
101 goto out; \
102 if (nargs - 1 < (arg_min)) \
103 goto out; \
104 if (nargs - 1 > (arg_max)) \
105 goto out; \
106} while (0)
107
108#define OUT(msg) do { \
109 auth_defer_msg = (US msg); \
110 goto out; \
111} while(0)
112
7befa435
PH
113
114
14aa5a05 115/*************************************************
57c2c631
PH
116* "fgets" to read directly from socket *
117*************************************************/
118
119/* Added by PH after a suggestion by Steve Usher because the previous use of
120C-style buffered I/O gave trouble. */
121
122static uschar *
123dc_gets(uschar *s, int n, int fd)
124{
125int p = 0;
126int count = 0;
127
128for (;;)
129 {
130 if (sbp == 0)
131 {
132 sbp = read(fd, sbuffer, sizeof(sbuffer));
133 if (sbp == 0) { if (count == 0) return NULL; else break; }
134 }
135
136 while (p < sbp)
137 {
138 if (count >= n - 1) break;
139 s[count++] = sbuffer[p];
140 if (sbuffer[p++] == '\n') break;
141 }
142
143 memmove(sbuffer, sbuffer + p, sbp - p);
144 sbp -= p;
145
146 if (s[count-1] == '\n' || count >= n - 1) break;
147 }
148
149s[count] = 0;
150return s;
151}
152
153
154
155
156/*************************************************
157* Server entry point *
158*************************************************/
14aa5a05
PH
159
160int auth_dovecot_server(auth_instance *ablock, uschar *data)
161{
162 auth_dovecot_options_block *ob =
163 (auth_dovecot_options_block *)(ablock->options_block);
164 struct sockaddr_un sa;
57c2c631
PH
165 uschar buffer[4096];
166 uschar *args[8];
7befa435
PH
167 uschar *auth_command;
168 uschar *auth_extra_data = US"";
14aa5a05
PH
169 int nargs, tmp;
170 int cuid = 0, cont = 1, found = 0, fd, ret = DEFER;
14aa5a05
PH
171
172 HDEBUG(D_auth) debug_printf("dovecot authentication\n");
173
174 memset(&sa, 0, sizeof(sa));
175 sa.sun_family = AF_UNIX;
176
177 /* This was the original code here: it is nonsense because strncpy()
178 does not return an integer. I have converted this to use the function
179 that formats and checks length. PH */
180
181 /*
182 if (strncpy(sa.sun_path, ob->server_socket, sizeof(sa.sun_path)) < 0) {
183 */
184
185 if (!string_format(US sa.sun_path, sizeof(sa.sun_path), "%s",
186 ob->server_socket)) {
187 auth_defer_msg = US"authentication socket path too long";
188 return DEFER;
189 }
190
191 auth_defer_msg = US"authentication socket connection error";
192
193 fd = socket(PF_UNIX, SOCK_STREAM, 0);
194 if (fd < 0)
195 return DEFER;
196
197 if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
198 goto out;
199
14aa5a05
PH
200 auth_defer_msg = US"authentication socket protocol error";
201
57c2c631 202 sbp = 0; /* Socket buffer pointer */
14aa5a05 203 while (cont) {
57c2c631 204 if (dc_gets(buffer, sizeof(buffer), fd) == NULL)
14aa5a05
PH
205 OUT("authentication socket read error or premature eof");
206
57c2c631 207 buffer[Ustrlen(buffer) - 1] = 0;
14aa5a05
PH
208 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
209 nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
210
211 switch (toupper(*args[0])) {
212 case 'C':
213 CHECK_COMMAND("CUID", 1, 1);
57c2c631 214 cuid = Uatoi(args[1]);
14aa5a05
PH
215 break;
216
217 case 'D':
218 CHECK_COMMAND("DONE", 0, 0);
219 cont = 0;
220 break;
221
222 case 'M':
223 CHECK_COMMAND("MECH", 1, INT_MAX);
224 if (strcmpic(US args[1], ablock->public_name) == 0)
225 found = 1;
226 break;
227
228 case 'S':
229 CHECK_COMMAND("SPID", 1, 1);
230 break;
231
232 case 'V':
233 CHECK_COMMAND("VERSION", 2, 2);
57c2c631 234 if (Uatoi(args[1]) != VERSION_MAJOR)
14aa5a05
PH
235 OUT("authentication socket protocol version mismatch");
236 break;
237
238 default:
239 goto out;
240 }
241 }
242
243 if (!found)
244 goto out;
245
7befa435
PH
246 /* Added by PH: data must not contain tab (as it is
247 b64 it shouldn't, but check for safety). */
248
249 if (Ustrchr(data, '\t') != NULL) {
250 ret = FAIL;
251 goto out;
252 }
253
254 /* Added by PH: extra fields when TLS is in use or if the TCP/IP
255 connection is local. */
256
257 if (tls_cipher != NULL)
258 auth_extra_data = string_sprintf("secured\t%s%s",
259 tls_certificate_verified? "valid-client-cert" : "",
260 tls_certificate_verified? "\t" : "");
094a9ec3
PH
261 else if (interface_address != NULL &&
262 Ustrcmp(sender_host_address, interface_address) == 0)
7befa435
PH
263 auth_extra_data = US"secured\t";
264
14aa5a05
PH
265
266/****************************************************************************
267 The code below was the original code here. It didn't work. A reading of the
268 file auth-protocol.txt.gz that came with Dovecot 1.0_beta8 indicated that
7befa435
PH
269 this was not right. Maybe something changed. I changed it to move the
270 service indication into the AUTH command, and it seems to be better. PH
14aa5a05
PH
271
272 fprintf(f, "VERSION\t%d\t%d\r\nSERVICE\tSMTP\r\nCPID\t%d\r\n"
273 "AUTH\t%d\t%s\trip=%s\tlip=%s\tresp=%s\r\n",
274 VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
275 ablock->public_name, sender_host_address, interface_address,
276 data ? (char *) data : "");
7befa435
PH
277
278 Subsequently, the command was modified to add "secured" and "valid-client-
279 cert" when relevant.
14aa5a05
PH
280****************************************************************************/
281
7befa435
PH
282 auth_command = string_sprintf("VERSION\t%d\t%d\nCPID\t%d\n"
283 "AUTH\t%d\t%s\tservice=smtp\t%srip=%s\tlip=%s\tresp=%s\n",
14aa5a05 284 VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
7befa435
PH
285 ablock->public_name, auth_extra_data, sender_host_address,
286 interface_address, data ? (char *) data : "");
14aa5a05 287
57c2c631
PH
288 if (write(fd, auth_command, Ustrlen(auth_command)) < 0)
289 HDEBUG(D_auth) debug_printf("error sending auth_command: %s\n",
290 strerror(errno));
291
7befa435 292 HDEBUG(D_auth) debug_printf("sent: %s", auth_command);
14aa5a05
PH
293
294 while (1) {
57c2c631
PH
295 uschar *temp;
296 if (dc_gets(buffer, sizeof(buffer), fd) == NULL) {
14aa5a05
PH
297 auth_defer_msg = US"authentication socket read error or premature eof";
298 goto out;
299 }
300
57c2c631 301 buffer[Ustrlen(buffer) - 1] = 0;
14aa5a05
PH
302 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
303 nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
304
57c2c631 305 if (Uatoi(args[1]) != cuid)
14aa5a05
PH
306 OUT("authentication socket connection id mismatch");
307
308 switch (toupper(*args[0])) {
309 case 'C':
310 CHECK_COMMAND("CONT", 1, 2);
311
312 tmp = auth_get_no64_data(&data, US args[2]);
313 if (tmp != OK) {
314 ret = tmp;
315 goto out;
316 }
317
7befa435
PH
318 /* Added by PH: data must not contain tab (as it is
319 b64 it shouldn't, but check for safety). */
320
321 if (Ustrchr(data, '\t') != NULL) {
322 ret = FAIL;
323 goto out;
324 }
325
57c2c631
PH
326 temp = string_sprintf("CONT\t%d\t%s\r\n", cuid, data);
327 if (write(fd, temp, Ustrlen(temp)) < 0)
14aa5a05 328 OUT("authentication socket write error");
14aa5a05
PH
329 break;
330
331 case 'F':
332 CHECK_COMMAND("FAIL", 1, 2);
333
334 /* FIXME: add proper response handling */
335 if (args[2]) {
57c2c631 336 uschar *p = Ustrchr(args[2], '=');
14aa5a05
PH
337 if (p) {
338 ++p;
f0872424
PH
339 expand_nstring[1] = auth_vars[0] =
340 string_copy(p); /* PH */
14aa5a05
PH
341 expand_nlength[1] = Ustrlen(p);
342 expand_nmax = 1;
343 }
344 }
345
346 ret = FAIL;
347 goto out;
348
349 case 'O':
350 CHECK_COMMAND("OK", 2, 2);
351 {
352 /* FIXME: add proper response handling */
57c2c631 353 uschar *p = Ustrchr(args[2], '=');
14aa5a05
PH
354 if (!p)
355 OUT("authentication socket protocol error, username missing");
356
357 p++;
f0872424
PH
358 expand_nstring[1] = auth_vars[0] =
359 string_copy(p); /* PH */
14aa5a05
PH
360 expand_nlength[1] = Ustrlen(p);
361 expand_nmax = 1;
362 }
363 ret = OK;
364 /* fallthrough */
365
366 default:
367 goto out;
368 }
369 }
370
57c2c631 371out:
16ff981e
PH
372
373 /* Expand server_condition as an authorization check */
374 return (ret == OK)? auth_check_serv_cond(ablock) : ret;
14aa5a05 375}