Use compressed form of ipv6 in $sender_host_address under -bh. Bug 3027
[exim.git] / src / src / auths / plaintext.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2020 - 2023 */
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(auth_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(auth_instance *ablock)
64 {
65 auth_plaintext_options_block *ob =
66   (auth_plaintext_options_block *)(ablock->options_block);
67 if (!ablock->public_name) ablock->public_name = ablock->name;
68 if (ablock->server_condition) ablock->server = TRUE;
69 if (ob->client_send) ablock->client = TRUE;
70 }
71
72
73
74 /*************************************************
75 *             Server entry point                 *
76 *************************************************/
77
78 /* For interface, see auths/README */
79
80 int
81 auth_plaintext_server(auth_instance * ablock, uschar * data)
82 {
83 auth_plaintext_options_block * ob =
84   (auth_plaintext_options_block *)(ablock->options_block);
85 const uschar * prompts = ob->server_prompts;
86 uschar * s;
87 int number = 1;
88 int rc;
89 int sep = 0;
90
91 /* Expand a non-empty list of prompt strings */
92
93 if (prompts)
94   if (!(prompts = expand_cstring(prompts)))
95     {
96     auth_defer_msg = expand_string_message;
97     return DEFER;
98     }
99
100 /* If data was supplied on the AUTH command, decode it, and split it up into
101 multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
102 up to a maximum. To retain backwards compatibility, they are also put int $1,
103 $2, etc. If the data consists of the string "=" it indicates a single, empty
104 string. */
105
106 if (*data)
107   if ((rc = auth_read_input(data)) != OK)
108     return rc;
109
110 /* Now go through the list of prompt strings. Skip over any whose data has
111 already been provided as part of the AUTH command. For the rest, send them
112 out as prompts, and get a data item back. If the data item is "*", abandon the
113 authentication attempt. Otherwise, split it into items as above. */
114
115 while (  (s = string_nextinlist(&prompts, &sep, NULL, 0))
116       && expand_nmax < EXPAND_MAXN)
117   if (number++ > expand_nmax)
118     if ((rc = auth_prompt(CUS s)) != OK)
119       return rc;
120
121 /* We now have a number of items of data in $auth1, $auth2, etc (and also, for
122 compatibility, in $1, $2, etc). Authentication and authorization are handled
123 together for this authenticator by expanding the server_condition option. Note
124 that ablock->server_condition is always non-NULL because that's what configures
125 this authenticator as a server. */
126
127 return auth_check_serv_cond(ablock);
128 }
129
130
131
132 /*************************************************
133 *              Client entry point                *
134 *************************************************/
135
136 /* For interface, see auths/README */
137
138 int
139 auth_plaintext_client(
140   auth_instance *ablock,                 /* authenticator block */
141   void * sx,                             /* smtp connextion */
142   int timeout,                           /* command timeout */
143   uschar *buffer,                        /* buffer for reading response */
144   int buffsize)                          /* size of buffer */
145 {
146 auth_plaintext_options_block *ob =
147   (auth_plaintext_options_block *)(ablock->options_block);
148 const uschar * text = ob->client_send;
149 const uschar * s;
150 int sep = 0;
151 int auth_var_idx = 0, rc;
152 int flags = AUTH_ITEM_FIRST;
153
154 if (ob->client_ignore_invalid_base64)
155   flags |= AUTH_ITEM_IGN64;
156
157 /* The text is broken up into a number of different data items, which are
158 sent one by one. The first one is sent with the AUTH command; the remainder are
159 sent in response to subsequent prompts. Each is expanded before being sent. */
160
161 while ((s = string_nextinlist(&text, &sep, NULL, 0)))
162   {
163   if (!text)
164     flags |= AUTH_ITEM_LAST;
165
166   if ((rc = auth_client_item(sx, ablock, &s, flags, timeout, buffer, buffsize))
167        != DEFER)
168     return rc;
169
170   flags &= ~AUTH_ITEM_FIRST;
171
172   if (auth_var_idx < AUTH_VARS)
173     auth_vars[auth_var_idx++] = string_copy(s);
174   }
175
176 /* Control should never actually get here. */
177
178 return FAIL;
179 }
180
181 #endif  /*!MACRO_PREDEF*/
182 #endif  /*AUTH_PLAINTEST*/
183 /* End of plaintext.c */