Authenticators: refactor SASL support code
[exim.git] / src / src / auths / plaintext.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "plaintext.h"
10
11
12/* Options specific to the plaintext authentication mechanism. */
13
14optionlist auth_plaintext_options[] = {
4730f942
PH
15 { "client_ignore_invalid_base64", opt_bool,
16 (void *)(offsetof(auth_plaintext_options_block, client_ignore_invalid_base64)) },
0756eb3c
PH
17 { "client_send", opt_stringptr,
18 (void *)(offsetof(auth_plaintext_options_block, client_send)) },
0756eb3c
PH
19 { "server_prompts", opt_stringptr,
20 (void *)(offsetof(auth_plaintext_options_block, server_prompts)) }
21};
22
23/* Size of the options list. An extern variable has to be used so that its
24address can appear in the tables drtables.c. */
25
26int auth_plaintext_options_count =
27 sizeof(auth_plaintext_options)/sizeof(optionlist);
28
29/* Default private options block for the plaintext authentication method. */
30
31auth_plaintext_options_block auth_plaintext_option_defaults = {
0756eb3c 32 NULL, /* server_prompts */
4730f942
PH
33 NULL, /* client_send */
34 FALSE /* client_ignore_invalid_base64 */
0756eb3c
PH
35};
36
37
d185889f
JH
38#ifdef MACRO_PREDEF
39
40/* Dummy values */
41void auth_plaintext_init(auth_instance *ablock) {}
42int auth_plaintext_server(auth_instance *ablock, uschar *data) {return 0;}
251b9eb4
JH
43int auth_plaintext_client(auth_instance *ablock, void * sx, int timeout,
44 uschar *buffer, int buffsize) {return 0;}
d185889f
JH
45
46#else /*!MACRO_PREDEF*/
47
48
49
0756eb3c
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. */
57
58void
59auth_plaintext_init(auth_instance *ablock)
60{
61auth_plaintext_options_block *ob =
62 (auth_plaintext_options_block *)(ablock->options_block);
63if (ablock->public_name == NULL) ablock->public_name = ablock->name;
16ff981e 64if (ablock->server_condition != NULL) ablock->server = TRUE;
0756eb3c
PH
65if (ob->client_send != NULL) ablock->client = TRUE;
66}
67
68
69
70/*************************************************
71* Server entry point *
72*************************************************/
73
74/* For interface, see auths/README */
75
76int
77auth_plaintext_server(auth_instance *ablock, uschar *data)
78{
79auth_plaintext_options_block *ob =
80 (auth_plaintext_options_block *)(ablock->options_block);
55414b25 81const uschar *prompts = ob->server_prompts;
16ff981e 82uschar *clear, *end, *s;
0756eb3c
PH
83int number = 1;
84int len, rc;
85int sep = 0;
86
87/* Expand a non-empty list of prompt strings */
88
a310a8d0
JH
89if (prompts)
90 if (!(prompts = expand_cstring(prompts)))
0756eb3c
PH
91 {
92 auth_defer_msg = expand_string_message;
93 return DEFER;
94 }
0756eb3c
PH
95
96/* If data was supplied on the AUTH command, decode it, and split it up into
f78eb7c6
PH
97multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
98up to a maximum. To retain backwards compatibility, they are also put int $1,
99$2, etc. If the data consists of the string "=" it indicates a single, empty
100string. */
0756eb3c 101
a310a8d0
JH
102if (*data)
103 if ((rc = auth_read_input(data)) != OK)
104 return rc;
0756eb3c
PH
105
106/* Now go through the list of prompt strings. Skip over any whose data has
107already been provided as part of the AUTH command. For the rest, send them
108out as prompts, and get a data item back. If the data item is "*", abandon the
109authentication attempt. Otherwise, split it into items as above. */
110
a310a8d0
JH
111while ( (s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
112 && expand_nmax < EXPAND_MAXN)
113 if (number++ > expand_nmax)
114 if ((rc = auth_prompt(CUS s)) != OK)
115 return rc;
0756eb3c 116
f78eb7c6 117/* We now have a number of items of data in $auth1, $auth2, etc (and also, for
16ff981e
PH
118compatibility, in $1, $2, etc). Authentication and authorization are handled
119together for this authenticator by expanding the server_condition option. Note
120that ablock->server_condition is always non-NULL because that's what configures
121this authenticator as a server. */
0756eb3c 122
16ff981e 123return auth_check_serv_cond(ablock);
0756eb3c
PH
124}
125
126
127
128/*************************************************
129* Client entry point *
130*************************************************/
131
132/* For interface, see auths/README */
133
134int
135auth_plaintext_client(
136 auth_instance *ablock, /* authenticator block */
251b9eb4 137 void * sx, /* smtp connextion */
0756eb3c
PH
138 int timeout, /* command timeout */
139 uschar *buffer, /* buffer for reading response */
140 int buffsize) /* size of buffer */
141{
142auth_plaintext_options_block *ob =
143 (auth_plaintext_options_block *)(ablock->options_block);
a310a8d0
JH
144const uschar * text = ob->client_send;
145const uschar * s;
0756eb3c
PH
146BOOL first = TRUE;
147int sep = 0;
a310a8d0
JH
148int auth_var_idx = 0, rc;
149int flags = AUTH_ITEM_FIRST;
150
151if (ob->client_ignore_invalid_base64)
152 flags |= AUTH_ITEM_IGN64;
0756eb3c
PH
153
154/* The text is broken up into a number of different data items, which are
155sent one by one. The first one is sent with the AUTH command; the remainder are
156sent in response to subsequent prompts. Each is expanded before being sent. */
157
a310a8d0 158while ((s = string_nextinlist(&text, &sep, NULL, 0)))
0756eb3c 159 {
251b9eb4 160 if (!text)
a310a8d0 161 flags |= AUTH_ITEM_LAST;
4730f942 162
a310a8d0
JH
163 if ((rc = auth_client_item(sx, ablock, &s, flags, timeout, buffer, buffsize))
164 != DEFER)
165 return rc;
4730f942 166
a310a8d0 167 flags &= ~AUTH_ITEM_FIRST;
4730f942
PH
168
169 if (auth_var_idx < AUTH_VARS)
a310a8d0 170 auth_vars[auth_var_idx++] = string_copy(s);
0756eb3c
PH
171 }
172
173/* Control should never actually get here. */
174
175return FAIL;
176}
177
d185889f 178#endif /*!MACRO_PREDEF*/
0756eb3c 179/* End of plaintext.c */