Coverage Report

Created: 2026-01-10 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vauth/vauth.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "../curl_setup.h"
25
26
#include "vauth.h"
27
#include "../curlx/multibyte.h"
28
#include "../url.h"
29
30
/*
31
 * Curl_auth_build_spn()
32
 *
33
 * This is used to build an SPN string in the following formats:
34
 *
35
 * service/host@realm (Not currently used)
36
 * service/host       (Not used by GSS-API)
37
 * service@realm      (Not used by Windows SSPI)
38
 *
39
 * Parameters:
40
 *
41
 * service  [in] - The service type such as http, smtp, pop or imap.
42
 * host     [in] - The hostname.
43
 * realm    [in] - The realm.
44
 *
45
 * Returns a pointer to the newly allocated SPN.
46
 */
47
#ifndef USE_WINDOWS_SSPI
48
char *Curl_auth_build_spn(const char *service, const char *host,
49
                          const char *realm)
50
0
{
51
0
  char *spn = NULL;
52
53
  /* Generate our SPN */
54
0
  if(host && realm)
55
0
    spn = curl_maprintf("%s/%s@%s", service, host, realm);
56
0
  else if(host)
57
0
    spn = curl_maprintf("%s/%s", service, host);
58
0
  else if(realm)
59
0
    spn = curl_maprintf("%s@%s", service, realm);
60
61
  /* Return our newly allocated SPN */
62
0
  return spn;
63
0
}
64
#else
65
TCHAR *Curl_auth_build_spn(const char *service, const char *host,
66
                           const char *realm)
67
{
68
  char *utf8_spn = NULL;
69
  TCHAR *tchar_spn = NULL;
70
71
  (void)realm;
72
73
  /* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather
74
     than doing this ourselves but the first is only available in Windows XP
75
     and Windows Server 2003 and the latter is only available in Windows 2000
76
     but not Windows95/98/ME or Windows NT4.0 unless the Active Directory
77
     Client Extensions are installed. As such it is far simpler for us to
78
     formulate the SPN instead. */
79
80
  /* Generate our UTF8 based SPN */
81
  utf8_spn = curl_maprintf("%s/%s", service, host);
82
  if(!utf8_spn)
83
    return NULL;
84
85
  /* Allocate and return a TCHAR based SPN. */
86
  tchar_spn = curlx_convert_UTF8_to_tchar(utf8_spn);
87
  curlx_free(utf8_spn);
88
89
  return tchar_spn;
90
}
91
#endif /* USE_WINDOWS_SSPI */
92
93
/*
94
 * Curl_auth_user_contains_domain()
95
 *
96
 * This is used to test if the specified user contains a Windows domain name as
97
 * follows:
98
 *
99
 * Domain\User (Down-level Logon Name)
100
 * Domain/User (curl Down-level format - for compatibility with existing code)
101
 * User@Domain (User Principal Name)
102
 *
103
 * Note: The username may be empty when using a GSS-API library or Windows
104
 * SSPI as the user and domain are either obtained from the credentials cache
105
 * when using GSS-API or via the currently logged in user's credentials when
106
 * using Windows SSPI.
107
 *
108
 * Parameters:
109
 *
110
 * user  [in] - The username.
111
 *
112
 * Returns TRUE on success; otherwise FALSE.
113
 */
114
bool Curl_auth_user_contains_domain(const char *user)
115
0
{
116
0
  bool valid = FALSE;
117
118
0
  if(user && *user) {
119
    /* Check we have a domain name or UPN present */
120
0
    char *p = strpbrk(user, "\\/@");
121
122
0
    valid = (p != NULL && p > user && p < user + strlen(user) - 1);
123
0
  }
124
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
125
  else
126
    /* User and domain are obtained from the GSS-API credentials cache or the
127
       currently logged in user from Windows */
128
    valid = TRUE;
129
#endif
130
131
0
  return valid;
132
0
}
133
134
/*
135
 * Curl_auth_allowed_to_host() tells if authentication, cookies or other
136
 * "sensitive data" can (still) be sent to this host.
137
 */
138
bool Curl_auth_allowed_to_host(struct Curl_easy *data)
139
0
{
140
0
  struct connectdata *conn = data->conn;
141
0
  return !data->state.this_is_a_follow ||
142
0
         data->set.allow_auth_to_other_hosts ||
143
0
         (data->state.first_host &&
144
0
          curl_strequal(data->state.first_host, conn->host.name) &&
145
0
          (data->state.first_remote_port == conn->remote_port) &&
146
0
          (data->state.first_remote_protocol == conn->handler->protocol));
147
0
}
148
149
#ifdef USE_NTLM
150
151
static void ntlm_conn_dtor(void *key, size_t klen, void *entry)
152
0
{
153
0
  struct ntlmdata *ntlm = entry;
154
0
  (void)key;
155
0
  (void)klen;
156
0
  DEBUGASSERT(ntlm);
157
0
  Curl_auth_cleanup_ntlm(ntlm);
158
0
  curlx_free(ntlm);
159
0
}
160
161
struct ntlmdata *Curl_auth_ntlm_get(struct connectdata *conn, bool proxy)
162
0
{
163
0
  const char *key = proxy ? CURL_META_NTLM_PROXY_CONN : CURL_META_NTLM_CONN;
164
0
  struct ntlmdata *ntlm = Curl_conn_meta_get(conn, key);
165
0
  if(!ntlm) {
166
0
    ntlm = curlx_calloc(1, sizeof(*ntlm));
167
0
    if(!ntlm || Curl_conn_meta_set(conn, key, ntlm, ntlm_conn_dtor))
168
0
      return NULL;
169
0
  }
170
0
  return ntlm;
171
0
}
172
173
void Curl_auth_ntlm_remove(struct connectdata *conn, bool proxy)
174
0
{
175
0
  Curl_conn_meta_remove(conn, proxy ? CURL_META_NTLM_PROXY_CONN
176
0
                                    : CURL_META_NTLM_CONN);
177
0
}
178
179
#endif /* USE_NTLM */
180
181
#ifdef USE_KERBEROS5
182
183
static void krb5_conn_dtor(void *key, size_t klen, void *entry)
184
{
185
  struct kerberos5data *krb5 = entry;
186
  (void)key;
187
  (void)klen;
188
  DEBUGASSERT(krb5);
189
  Curl_auth_cleanup_gssapi(krb5);
190
  curlx_free(krb5);
191
}
192
193
struct kerberos5data *Curl_auth_krb5_get(struct connectdata *conn)
194
{
195
  struct kerberos5data *krb5 = Curl_conn_meta_get(conn, CURL_META_KRB5_CONN);
196
  if(!krb5) {
197
    krb5 = curlx_calloc(1, sizeof(*krb5));
198
    if(!krb5 ||
199
       Curl_conn_meta_set(conn, CURL_META_KRB5_CONN, krb5, krb5_conn_dtor))
200
      return NULL;
201
  }
202
  return krb5;
203
}
204
205
#endif /* USE_KERBEROS5 */
206
207
#ifdef USE_GSASL
208
209
static void gsasl_conn_dtor(void *key, size_t klen, void *entry)
210
{
211
  struct gsasldata *gsasl = entry;
212
  (void)key;
213
  (void)klen;
214
  DEBUGASSERT(gsasl);
215
  Curl_auth_gsasl_cleanup(gsasl);
216
  curlx_free(gsasl);
217
}
218
219
struct gsasldata *Curl_auth_gsasl_get(struct connectdata *conn)
220
{
221
  struct gsasldata *gsasl = Curl_conn_meta_get(conn, CURL_META_GSASL_CONN);
222
  if(!gsasl) {
223
    gsasl = curlx_calloc(1, sizeof(*gsasl));
224
    if(!gsasl ||
225
       Curl_conn_meta_set(conn, CURL_META_GSASL_CONN, gsasl, gsasl_conn_dtor))
226
      return NULL;
227
  }
228
  return gsasl;
229
}
230
231
#endif /* USE_GSASL */
232
233
#ifdef USE_SPNEGO
234
235
static void nego_conn_dtor(void *key, size_t klen, void *entry)
236
{
237
  struct negotiatedata *nego = entry;
238
  (void)key;
239
  (void)klen;
240
  DEBUGASSERT(nego);
241
  Curl_auth_cleanup_spnego(nego);
242
  curlx_free(nego);
243
}
244
245
struct negotiatedata *Curl_auth_nego_get(struct connectdata *conn, bool proxy)
246
{
247
  const char *key = proxy ? CURL_META_NEGO_PROXY_CONN : CURL_META_NEGO_CONN;
248
  struct negotiatedata *nego = Curl_conn_meta_get(conn, key);
249
  if(!nego) {
250
    nego = curlx_calloc(1, sizeof(*nego));
251
    if(!nego || Curl_conn_meta_set(conn, key, nego, nego_conn_dtor))
252
      return NULL;
253
  }
254
  return nego;
255
}
256
257
#endif /* USE_SPNEGO */