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