1 /* $Cambridge: exim/src/src/auths/dovecot.c,v 1.11 2009/10/26 13:18:54 nm4 Exp $ */
4 * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
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.
12 /* A number of modifications have been made to the original code. Originally I
13 commented them specially, but now they are getting quite extensive, so I have
14 ceased doing that. The biggest change is to use unbuffered I/O on the socket
15 because using C buffered I/O gives problems on some operating systems. PH */
20 #define VERSION_MAJOR 1
21 #define VERSION_MINOR 0
23 /* Options specific to the authentication mechanism. */
24 optionlist auth_dovecot_options[] = {
28 (void *)(offsetof(auth_dovecot_options_block, server_socket))
32 /* Size of the options list. An extern variable has to be used so that its
33 address can appear in the tables drtables.c. */
35 int auth_dovecot_options_count =
36 sizeof(auth_dovecot_options) / sizeof(optionlist);
38 /* Default private options block for the authentication method. */
40 auth_dovecot_options_block auth_dovecot_option_defaults = {
41 NULL, /* server_socket */
45 /* Static variables for reading from the socket */
47 static uschar sbuffer[256];
52 /*************************************************
53 * Initialization entry point *
54 *************************************************/
56 /* Called for each instance, after its options have been read, to
57 enable consistency checks to be done, or anything else that needs
60 void auth_dovecot_init(auth_instance *ablock)
62 auth_dovecot_options_block *ob =
63 (auth_dovecot_options_block *)(ablock->options_block);
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;
72 static int strcut(uschar *str, uschar **ptrs, int nptrs)
77 for (n = 0; n < nptrs; n++)
99 #define CHECK_COMMAND(str, arg_min, arg_max) do { \
100 if (strcmpic(US(str), args[0]) != 0) \
102 if (nargs - 1 < (arg_min)) \
104 if ( (arg_max != -1) && (nargs - 1 > (arg_max)) ) \
108 #define OUT(msg) do { \
109 auth_defer_msg = (US msg); \
115 /*************************************************
116 * "fgets" to read directly from socket *
117 *************************************************/
119 /* Added by PH after a suggestion by Steve Usher because the previous use of
120 C-style buffered I/O gave trouble. */
123 dc_gets(uschar *s, int n, int fd)
132 sbp = read(fd, sbuffer, sizeof(sbuffer));
133 if (sbp == 0) { if (count == 0) return NULL; else break; }
139 if (count >= n - 1) break;
140 s[count++] = sbuffer[p];
141 if (sbuffer[p++] == '\n') break;
144 memmove(sbuffer, sbuffer + p, sbp - p);
147 if (s[count-1] == '\n' || count >= n - 1) break;
157 /*************************************************
158 * Server entry point *
159 *************************************************/
161 int auth_dovecot_server(auth_instance *ablock, uschar *data)
163 auth_dovecot_options_block *ob =
164 (auth_dovecot_options_block *)(ablock->options_block);
165 struct sockaddr_un sa;
168 uschar *auth_command;
169 uschar *auth_extra_data = US"";
171 int cuid = 0, cont = 1, found = 0, fd, ret = DEFER;
173 HDEBUG(D_auth) debug_printf("dovecot authentication\n");
175 memset(&sa, 0, sizeof(sa));
176 sa.sun_family = AF_UNIX;
178 /* This was the original code here: it is nonsense because strncpy()
179 does not return an integer. I have converted this to use the function
180 that formats and checks length. PH */
183 if (strncpy(sa.sun_path, ob->server_socket, sizeof(sa.sun_path)) < 0) {
186 if (!string_format(US sa.sun_path, sizeof(sa.sun_path), "%s",
187 ob->server_socket)) {
188 auth_defer_msg = US"authentication socket path too long";
192 auth_defer_msg = US"authentication socket connection error";
194 fd = socket(PF_UNIX, SOCK_STREAM, 0);
198 if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
201 auth_defer_msg = US"authentication socket protocol error";
203 sbp = 0; /* Socket buffer pointer */
205 if (dc_gets(buffer, sizeof(buffer), fd) == NULL)
206 OUT("authentication socket read error or premature eof");
208 buffer[Ustrlen(buffer) - 1] = 0;
209 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
210 nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
212 switch (toupper(*args[0])) {
214 CHECK_COMMAND("CUID", 1, 1);
215 cuid = Uatoi(args[1]);
219 CHECK_COMMAND("DONE", 0, 0);
224 CHECK_COMMAND("MECH", 1, INT_MAX);
225 if (strcmpic(US args[1], ablock->public_name) == 0)
230 CHECK_COMMAND("SPID", 1, 1);
234 CHECK_COMMAND("VERSION", 2, 2);
235 if (Uatoi(args[1]) != VERSION_MAJOR)
236 OUT("authentication socket protocol version mismatch");
247 /* Added by PH: data must not contain tab (as it is
248 b64 it shouldn't, but check for safety). */
250 if (Ustrchr(data, '\t') != NULL) {
255 /* Added by PH: extra fields when TLS is in use or if the TCP/IP
256 connection is local. */
258 if (tls_cipher != NULL)
259 auth_extra_data = string_sprintf("secured\t%s%s",
260 tls_certificate_verified? "valid-client-cert" : "",
261 tls_certificate_verified? "\t" : "");
262 else if (interface_address != NULL &&
263 Ustrcmp(sender_host_address, interface_address) == 0)
264 auth_extra_data = US"secured\t";
267 /****************************************************************************
268 The code below was the original code here. It didn't work. A reading of the
269 file auth-protocol.txt.gz that came with Dovecot 1.0_beta8 indicated that
270 this was not right. Maybe something changed. I changed it to move the
271 service indication into the AUTH command, and it seems to be better. PH
273 fprintf(f, "VERSION\t%d\t%d\r\nSERVICE\tSMTP\r\nCPID\t%d\r\n"
274 "AUTH\t%d\t%s\trip=%s\tlip=%s\tresp=%s\r\n",
275 VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
276 ablock->public_name, sender_host_address, interface_address,
277 data ? (char *) data : "");
279 Subsequently, the command was modified to add "secured" and "valid-client-
282 The auth protocol is documented here:
283 http://wiki.dovecot.org/Authentication_Protocol
284 ****************************************************************************/
286 auth_command = string_sprintf("VERSION\t%d\t%d\nCPID\t%d\n"
287 "AUTH\t%d\t%s\tservice=smtp\t%srip=%s\tlip=%s\tnologin\tresp=%s\n",
288 VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
289 ablock->public_name, auth_extra_data, sender_host_address,
290 interface_address, data ? (char *) data : "");
292 if (write(fd, auth_command, Ustrlen(auth_command)) < 0)
293 HDEBUG(D_auth) debug_printf("error sending auth_command: %s\n",
296 HDEBUG(D_auth) debug_printf("sent: %s", auth_command);
300 uschar *auth_id_pre = NULL;
303 if (dc_gets(buffer, sizeof(buffer), fd) == NULL) {
304 auth_defer_msg = US"authentication socket read error or premature eof";
308 buffer[Ustrlen(buffer) - 1] = 0;
309 HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
310 nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
312 if (Uatoi(args[1]) != cuid)
313 OUT("authentication socket connection id mismatch");
315 switch (toupper(*args[0])) {
317 CHECK_COMMAND("CONT", 1, 2);
319 tmp = auth_get_no64_data(&data, US args[2]);
325 /* Added by PH: data must not contain tab (as it is
326 b64 it shouldn't, but check for safety). */
328 if (Ustrchr(data, '\t') != NULL) {
333 temp = string_sprintf("CONT\t%d\t%s\n", cuid, data);
334 if (write(fd, temp, Ustrlen(temp)) < 0)
335 OUT("authentication socket write error");
339 CHECK_COMMAND("FAIL", 1, -1);
341 for (i=2; (i<nargs) && (auth_id_pre == NULL); i++)
343 if ( Ustrncmp(args[i], US"user=", 5) == 0 )
345 auth_id_pre = args[i]+5;
346 expand_nstring[1] = auth_vars[0] =
347 string_copy(auth_id_pre); /* PH */
348 expand_nlength[1] = Ustrlen(auth_id_pre);
357 CHECK_COMMAND("OK", 2, -1);
360 * Search for the "user=$USER" string in the args array
361 * and return the proper value.
363 for (i=2; (i<nargs) && (auth_id_pre == NULL); i++)
365 if ( Ustrncmp(args[i], US"user=", 5) == 0 )
367 auth_id_pre = args[i]+5;
368 expand_nstring[1] = auth_vars[0] =
369 string_copy(auth_id_pre); /* PH */
370 expand_nlength[1] = Ustrlen(auth_id_pre);
375 if (auth_id_pre == NULL)
376 OUT("authentication socket protocol error, username missing");
387 /* close the socket used by dovecot */
391 /* Expand server_condition as an authorization check */
392 return (ret == OK)? auth_check_serv_cond(ablock) : ret;