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