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