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