Merge branch '4.next'
[exim.git] / src / src / auths / external.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2023 */
6 /* Copyright (c) Jeremy Harris 2019-2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
9
10 /* This file provides an Exim authenticator driver for
11 a server to verify a client SSL certificate, using the EXTERNAL
12 method defined in RFC 4422 Appendix A.
13 */
14
15
16 #include "../exim.h"
17 #include "external.h"
18
19 /* Options specific to the external authentication mechanism. */
20
21 optionlist auth_external_options[] = {
22   { "client_send",      opt_stringptr, OPT_OFF(auth_external_options_block, client_send) },
23   { "server_param2",    opt_stringptr, OPT_OFF(auth_external_options_block, server_param2) },
24   { "server_param3",    opt_stringptr, OPT_OFF(auth_external_options_block, server_param3) },
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_external_options_count = nelem(auth_external_options);
31
32 /* Default private options block for the authentication method. */
33
34 auth_external_options_block auth_external_option_defaults = {
35     .server_param2 = NULL,
36     .server_param3 = NULL,
37
38     .client_send = NULL,
39 };
40
41
42 #ifdef MACRO_PREDEF
43
44 /* Dummy values */
45 void auth_external_init(auth_instance *ablock) {}
46 int auth_external_server(auth_instance *ablock, uschar *data) {return 0;}
47 int auth_external_client(auth_instance *ablock, void * sx,
48   int timeout, uschar *buffer, int buffsize) {return 0;}
49
50 #else   /*!MACRO_PREDEF*/
51
52
53
54
55 /*************************************************
56 *          Initialization entry point            *
57 *************************************************/
58
59 /* Called for each instance, after its options have been read, to
60 enable consistency checks to be done, or anything else that needs
61 to be set up. */
62
63 void
64 auth_external_init(auth_instance *ablock)
65 {
66 auth_external_options_block * ob = (auth_external_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_external_server(auth_instance * ablock, uschar * data)
82 {
83 auth_external_options_block * ob = (auth_external_options_block *)ablock->options_block;
84 int rc;
85
86 /* If data was supplied on the AUTH command, decode it, and split it up into
87 multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
88 up to a maximum. To retain backwards compatibility, they are also put int $1,
89 $2, etc. If the data consists of the string "=" it indicates a single, empty
90 string. */
91
92 if (*data)
93   if ((rc = auth_read_input(data)) != OK)
94     return rc;
95
96 /* Now go through the list of prompt strings. Skip over any whose data has
97 already been provided as part of the AUTH command. For the rest, send them
98 out as prompts, and get a data item back. If the data item is "*", abandon the
99 authentication attempt. Otherwise, split it into items as above. */
100
101 if (expand_nmax == 0)   /* skip if rxd data */
102   if ((rc = auth_prompt(CUS"")) != OK)
103     return rc;
104
105 if (ob->server_param2)
106   {
107   uschar * s = expand_string(ob->server_param2);
108   auth_vars[expand_nmax = 1] = s;
109   expand_nstring[++expand_nmax] = s;
110   expand_nlength[expand_nmax] = Ustrlen(s);
111   if (ob->server_param3)
112     {
113     s = expand_string(ob->server_param3);
114     auth_vars[expand_nmax] = s;
115     expand_nstring[++expand_nmax] = s;
116     expand_nlength[expand_nmax] = Ustrlen(s);
117     }
118   }
119
120 return auth_check_serv_cond(ablock);
121 }
122
123
124
125 /*************************************************
126 *              Client entry point                *
127 *************************************************/
128
129 /* For interface, see auths/README */
130
131 int
132 auth_external_client(
133   auth_instance *ablock,                 /* authenticator block */
134   void * sx,                             /* smtp connextion */
135   int timeout,                           /* command timeout */
136   uschar *buffer,                        /* buffer for reading response */
137   int buffsize)                          /* size of buffer */
138 {
139 auth_external_options_block *ob =
140   (auth_external_options_block *)(ablock->options_block);
141 const uschar * text = ob->client_send;
142 int rc;
143
144 /* We output an AUTH command with one expanded argument, the client_send option */
145
146 if ((rc = auth_client_item(sx, ablock, &text, AUTH_ITEM_FIRST | AUTH_ITEM_LAST,
147       timeout, buffer, buffsize)) != OK)
148   return rc == DEFER ? FAIL : rc;
149
150 if (text) auth_vars[0] = string_copy(text);
151 return OK;
152 }
153
154
155
156 #endif   /*!MACRO_PREDEF*/
157 /* End of external.c */