2 * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
3 * Copyright (c) 2006-2017 The Exim Maintainers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 /* A number of modifications have been made to the original code. Originally I
12 commented them specially, but now they are getting quite extensive, so I have
13 ceased doing that. The biggest change is to use unbuffered I/O on the socket
14 because using C buffered I/O gives problems on some operating systems. PH */
16 /* Protocol specifications:
17 * Dovecot 1, protocol version 1.1
18 * http://wiki.dovecot.org/Authentication%20Protocol
20 * Dovecot 2, protocol version 1.1
21 * http://wiki2.dovecot.org/Design/AuthProtocol
27 #define VERSION_MAJOR 1
28 #define VERSION_MINOR 0
30 /* http://wiki.dovecot.org/Authentication%20Protocol
31 "The maximum line length isn't defined,
32 but it's currently expected to fit into 8192 bytes"
34 #define DOVECOT_AUTH_MAXLINELEN 8192
36 /* This was hard-coded as 8.
37 AUTH req C->S sends {"AUTH", id, mechanism, service } + params, 5 defined for
38 Dovecot 1; Dovecot 2 (same protocol version) defines 9.
40 Master->Server sends {"USER", id, userid} + params, 6 defined.
41 Server->Client only gives {"OK", id} + params, unspecified, only 1 guaranteed.
43 We only define here to accept S->C; max seen is 3+<unspecified>, plus the two
44 for the command and id, where unspecified might include _at least_ user=...
46 So: allow for more fields than we ever expect to see, while aware that count
47 can go up without changing protocol version.
48 The cost is the length of an array of pointers on the stack.
50 #define DOVECOT_AUTH_MAXFIELDCOUNT 16
52 /* Options specific to the authentication mechanism. */
53 optionlist auth_dovecot_options[] = {
54 { "server_socket", opt_stringptr,
55 OPT_OFF(auth_dovecot_options_block, server_socket)
59 /* Size of the options list. An extern variable has to be used so that its
60 address can appear in the tables drtables.c. */
62 int auth_dovecot_options_count = nelem(auth_dovecot_options);
64 /* Default private options block for the authentication method. */
66 auth_dovecot_options_block auth_dovecot_option_defaults = {
67 NULL, /* server_socket */
76 void auth_dovecot_init(auth_instance *ablock) {}
77 int auth_dovecot_server(auth_instance *ablock, uschar *data) {return 0;}
78 int auth_dovecot_client(auth_instance *ablock, void * sx,
79 int timeout, uschar *buffer, int buffsize) {return 0;}
81 #else /*!MACRO_PREDEF*/
84 /* Static variables for reading from the socket */
86 static uschar sbuffer[256];
87 static int socket_buffer_left;
91 /*************************************************
92 * Initialization entry point *
93 *************************************************/
95 /* Called for each instance, after its options have been read, to
96 enable consistency checks to be done, or anything else that needs
99 void auth_dovecot_init(auth_instance *ablock)
101 auth_dovecot_options_block *ob =
102 (auth_dovecot_options_block *)(ablock->options_block);
104 if (!ablock->public_name) ablock->public_name = ablock->name;
105 if (ob->server_socket) ablock->server = TRUE;
106 ablock->client = FALSE;
109 /*************************************************
110 * "strcut" to split apart server lines *
111 *************************************************/
113 /* Dovecot auth protocol uses TAB \t as delimiter; a line consists
114 of a command-name, TAB, and then any parameters, each separated by a TAB.
115 A parameter can be param=value or a bool, just param.
117 This function modifies the original str in-place, inserting NUL characters.
118 It initialises ptrs entries, setting all to NULL and only setting
119 non-NULL N entries, where N is the return value, the number of fields seen
120 (one more than the number of tabs).
122 Note that the return value will always be at least 1, is the count of
123 actual fields (so last valid offset into ptrs is one less).
127 strcut(uschar *str, uschar **ptrs, int nptrs)
129 uschar *last_sub_start = str;
132 for (n = 0; n < nptrs; n++)
140 *ptrs++ = last_sub_start;
141 last_sub_start = str;
145 /* It's acceptable for the string to end with a tab character. We see
146 this in AUTH PLAIN without an initial response from the client, which
147 causing us to send "334 " and get the data from the client. */
149 *ptrs = last_sub_start;
153 debug_printf("dovecot: warning: too many results from tab-splitting;"
154 " saw %d fields, room for %d\n", n, nptrs);
158 return n <= nptrs ? n : nptrs;
161 static void debug_strcut(uschar **ptrs, int nlen, int alen) ARG_UNUSED;
163 debug_strcut(uschar **ptrs, int nlen, int alen)
166 debug_printf("%d read but unreturned bytes; strcut() gave %d results: ",
167 socket_buffer_left, nlen);
168 for (i = 0; i < nlen; i++)
169 debug_printf(" {%s}", ptrs[i]);
171 debug_printf(" last is %s\n", ptrs[i] ? ptrs[i] : US"<null>");
173 debug_printf(" (max for capacity)\n");
176 #define CHECK_COMMAND(str, arg_min, arg_max) do { \
177 if (strcmpic(US(str), args[0]) != 0) \
179 if (nargs - 1 < (arg_min)) \
181 if ( (arg_max != -1) && (nargs - 1 > (arg_max)) ) \
185 #define OUT(msg) do { \
186 auth_defer_msg = (US msg); \
192 /*************************************************
193 * "fgets" to read directly from socket *
194 *************************************************/
196 /* Added by PH after a suggestion by Steve Usher because the previous use of
197 C-style buffered I/O gave trouble. */
200 dc_gets(uschar *s, int n, int fd)
207 if (socket_buffer_left == 0)
209 if ((socket_buffer_left = read(fd, sbuffer, sizeof(sbuffer))) <= 0)
210 if (count == 0) return NULL; else break;
214 while (p < socket_buffer_left)
216 if (count >= n - 1) break;
217 s[count++] = sbuffer[p];
218 if (sbuffer[p++] == '\n') break;
221 memmove(sbuffer, sbuffer + p, socket_buffer_left - p);
222 socket_buffer_left -= p;
224 if (s[count-1] == '\n' || count >= n - 1) break;
234 /*************************************************
235 * Server entry point *
236 *************************************************/
239 auth_dovecot_server(auth_instance * ablock, uschar * data)
241 auth_dovecot_options_block *ob =
242 (auth_dovecot_options_block *) ablock->options_block;
243 struct sockaddr_un sa;
244 uschar buffer[DOVECOT_AUTH_MAXLINELEN];
245 uschar *args[DOVECOT_AUTH_MAXFIELDCOUNT];
246 uschar *auth_command;
247 uschar *auth_extra_data = US"";
250 int crequid = 1, cont = 1, fd = -1, ret = DEFER;
251 BOOL found = FALSE, have_mech_line = FALSE;
253 HDEBUG(D_auth) debug_printf("dovecot authentication\n");
261 memset(&sa, 0, sizeof(sa));
262 sa.sun_family = AF_UNIX;
264 /* This was the original code here: it is nonsense because strncpy()
265 does not return an integer. I have converted this to use the function
266 that formats and checks length. PH */
269 if (strncpy(sa.sun_path, ob->server_socket, sizeof(sa.sun_path)) < 0) {
273 if (!string_format(US sa.sun_path, sizeof(sa.sun_path), "%s",
276 auth_defer_msg = US"authentication socket path too long";
280 auth_defer_msg = US"authentication socket connection error";
282 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
285 if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
288 auth_defer_msg = US"authentication socket protocol error";
290 socket_buffer_left = 0; /* Global, used to read more than a line but return by line */
293 if (!dc_gets(buffer, sizeof(buffer), fd))
294 OUT("authentication socket read error or premature eof");
295 p = buffer + Ustrlen(buffer) - 1;
297 OUT("authentication socket protocol line too long");
300 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
302 nargs = strcut(buffer, args, nelem(args));
304 /* HDEBUG(D_auth) debug_strcut(args, nargs, nelem(args)); */
306 /* Code below rewritten by Kirill Miazine (km@krot.org). Only check commands that
307 Exim will need. Original code also failed if Dovecot server sent unknown
308 command. E.g. COOKIE in version 1.1 of the protocol would cause troubles. */
309 /* pdp: note that CUID is a per-connection identifier sent by the server,
310 which increments at server discretion.
311 By contrast, the "id" field of the protocol is a connection-specific request
312 identifier, which needs to be unique per request from the client and is not
313 connected to the CUID value, so we ignore CUID from server. It's purely for
316 if (Ustrcmp(args[0], US"VERSION") == 0)
318 CHECK_COMMAND("VERSION", 2, 2);
319 if (Uatoi(args[1]) != VERSION_MAJOR)
320 OUT("authentication socket protocol version mismatch");
322 else if (Ustrcmp(args[0], US"MECH") == 0)
324 CHECK_COMMAND("MECH", 1, INT_MAX);
325 have_mech_line = TRUE;
326 if (strcmpic(US args[1], ablock->public_name) == 0)
329 else if (Ustrcmp(args[0], US"SPID") == 0)
331 /* Unfortunately the auth protocol handshake wasn't designed well
332 to differentiate between auth-client/userdb/master. auth-userdb
333 and auth-master send VERSION + SPID lines only and nothing
334 afterwards, while auth-client sends VERSION + MECH + SPID +
335 CUID + more. The simplest way that we can determine if we've
336 connected to the correct socket is to see if MECH line exists or
337 not (alternatively we'd have to have a small timeout after SPID
338 to see if CUID is sent or not). */
341 OUT("authentication socket type mismatch"
342 " (connected to auth-master instead of auth-client)");
344 else if (Ustrcmp(args[0], US"DONE") == 0)
346 CHECK_COMMAND("DONE", 0, 0);
353 auth_defer_msg = string_sprintf(
354 "Dovecot did not advertise mechanism \"%s\" to us", ablock->public_name);
358 /* Added by PH: data must not contain tab (as it is
359 b64 it shouldn't, but check for safety). */
361 if (Ustrchr(data, '\t') != NULL)
367 /* Added by PH: extra fields when TLS is in use or if the TCP/IP
368 connection is local. */
371 auth_extra_data = string_sprintf("secured\t%s%s",
372 tls_in.certificate_verified ? "valid-client-cert" : "",
373 tls_in.certificate_verified ? "\t" : "");
375 else if ( interface_address
376 && Ustrcmp(sender_host_address, interface_address) == 0)
377 auth_extra_data = US"secured\t";
380 /****************************************************************************
381 The code below was the original code here. It didn't work. A reading of the
382 file auth-protocol.txt.gz that came with Dovecot 1.0_beta8 indicated that
383 this was not right. Maybe something changed. I changed it to move the
384 service indication into the AUTH command, and it seems to be better. PH
386 fprintf(f, "VERSION\t%d\t%d\r\nSERVICE\tSMTP\r\nCPID\t%d\r\n"
387 "AUTH\t%d\t%s\trip=%s\tlip=%s\tresp=%s\r\n",
388 VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
389 ablock->public_name, sender_host_address, interface_address,
390 data ? CS data : "");
392 Subsequently, the command was modified to add "secured" and "valid-client-
394 ****************************************************************************/
396 auth_command = string_sprintf("VERSION\t%d\t%d\nCPID\t%d\n"
397 "AUTH\t%d\t%s\tservice=smtp\t%srip=%s\tlip=%s\tnologin\tresp=%s\n",
398 VERSION_MAJOR, VERSION_MINOR, getpid(), crequid,
399 ablock->public_name, auth_extra_data, sender_host_address,
400 interface_address, data);
402 if (write(fd, auth_command, Ustrlen(auth_command)) < 0)
403 HDEBUG(D_auth) debug_printf("error sending auth_command: %s\n",
406 HDEBUG(D_auth) debug_printf("sent: %s", auth_command);
411 uschar *auth_id_pre = NULL;
413 if (!dc_gets(buffer, sizeof(buffer), fd))
415 auth_defer_msg = US"authentication socket read error or premature eof";
419 buffer[Ustrlen(buffer) - 1] = 0;
420 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
421 nargs = strcut(buffer, args, nelem(args));
423 if (Uatoi(args[1]) != crequid)
424 OUT("authentication socket connection id mismatch");
426 switch (toupper(*args[0]))
429 CHECK_COMMAND("CONT", 1, 2);
431 if ((tmp = auth_get_no64_data(&data, US args[2])) != OK)
437 /* Added by PH: data must not contain tab (as it is
438 b64 it shouldn't, but check for safety). */
440 if (Ustrchr(data, '\t') != NULL)
446 temp = string_sprintf("CONT\t%d\t%s\n", crequid, data);
447 if (write(fd, temp, Ustrlen(temp)) < 0)
448 OUT("authentication socket write error");
452 CHECK_COMMAND("FAIL", 1, -1);
454 for (int i = 2; i < nargs && !auth_id_pre; i++)
455 if (Ustrncmp(args[i], US"user=", 5) == 0)
457 auth_id_pre = args[i] + 5;
458 expand_nstring[1] = auth_vars[0] = string_copy(auth_id_pre); /* PH */
459 expand_nlength[1] = Ustrlen(auth_id_pre);
466 CHECK_COMMAND("OK", 2, -1);
468 /* Search for the "user=$USER" string in the args array
469 and return the proper value. */
471 for (int i = 2; i < nargs && !auth_id_pre; i++)
472 if (Ustrncmp(args[i], US"user=", 5) == 0)
474 auth_id_pre = args[i] + 5;
475 expand_nstring[1] = auth_vars[0] = string_copy(auth_id_pre); /* PH */
476 expand_nlength[1] = Ustrlen(auth_id_pre);
481 OUT("authentication socket protocol error, username missing");
492 /* close the socket used by dovecot */
496 /* Expand server_condition as an authorization check */
497 return ret == OK ? auth_check_serv_cond(ablock) : ret;
501 #endif /*!MACRO_PREDEF*/