Coverage Report

Created: 2026-09-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/creds.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
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 <stddef.h>  /* for offsetof() */
27
28
#include "creds.h"
29
#include "curl_trc.h"
30
#include "strcase.h"
31
#include "urldata.h"
32
33
34
CURLcode Curl_creds_create(const char *user,
35
                           const char *passwd,
36
                           const char *oauth_bearer,
37
                           const char *sasl_authzid,
38
                           const char *sasl_service,
39
                           uint8_t source,
40
                           struct Curl_creds **pcreds)
41
0
{
42
0
  struct Curl_creds *creds = NULL;
43
0
  size_t ulen = user ? strlen(user) : 0;
44
0
  size_t plen = passwd ? strlen(passwd) : 0;
45
0
  size_t olen = oauth_bearer ? strlen(oauth_bearer) : 0;
46
0
  size_t salen = sasl_authzid ? strlen(sasl_authzid) : 0;
47
0
  size_t sslen = sasl_service ? strlen(sasl_service) : 0;
48
0
  char *s, *buf;
49
0
  size_t bufsize;
50
0
  CURLcode result = CURLE_OK;
51
52
0
  Curl_creds_unlink(pcreds);
53
54
  /* Everything empty/NULL, this is the NULL credential */
55
0
  if(!user && !passwd && !olen && !salen && !sslen)
56
0
    goto out;
57
58
0
  if((ulen > CURL_MAX_INPUT_LENGTH) ||
59
0
     (plen > CURL_MAX_INPUT_LENGTH) ||
60
0
     (olen > CURL_MAX_INPUT_LENGTH) ||
61
0
     (salen > CURL_MAX_INPUT_LENGTH) ||
62
0
     (sslen > CURL_MAX_INPUT_LENGTH)) {
63
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
64
0
    goto out;
65
0
  }
66
67
  /* null-terminator for user already part of struct */
68
0
  bufsize = ulen + plen + 1 + olen + 1 + salen + 1 + sslen + 1;
69
0
  creds = curlx_calloc(1, sizeof(*creds) + bufsize);
70
0
  if(!creds) {
71
0
    result = CURLE_OUT_OF_MEMORY;
72
0
    goto out;
73
0
  }
74
75
0
  creds->bufsize = bufsize;
76
0
  creds->refcount = 1;
77
0
  creds->source = source;
78
  /* Some compilers try to be too smart about our dynamic struct size */
79
0
  buf = ((char *)creds) + offsetof(struct Curl_creds, buf);
80
0
  creds->user = s = buf;
81
0
  if(ulen)
82
0
    memcpy(s, user, ulen + 1);
83
0
  creds->passwd = s = buf + ulen + 1;
84
0
  if(plen)
85
0
    memcpy(s, passwd, plen + 1);
86
0
  creds->oauth_bearer = s = buf + ulen + 1 + plen + 1;
87
0
  if(olen)
88
0
    memcpy(s, oauth_bearer, olen + 1);
89
0
  creds->sasl_authzid = s = buf + ulen + 1 + plen + 1 + olen + 1;
90
0
  if(salen)
91
0
    memcpy(s, sasl_authzid, salen + 1);
92
0
  creds->sasl_service = s = buf + ulen + 1 + plen + 1 + olen + 1 + salen + 1;
93
0
  if(sslen)
94
0
    memcpy(s, sasl_service, sslen + 1);
95
96
0
out:
97
0
  if(!result)
98
0
    *pcreds = creds;
99
0
  else
100
0
    Curl_creds_unlink(&creds);
101
0
  return result;
102
0
}
103
104
CURLcode Curl_creds_merge(const char *user,
105
                          const char *passwd,
106
                          struct Curl_creds *creds_in,
107
                          uint8_t source,
108
                          struct Curl_creds **pcreds_out)
109
0
{
110
0
  struct Curl_creds *creds_out = NULL;
111
0
  CURLcode result;
112
113
0
  if(!creds_in) {
114
0
    result = Curl_creds_create(user, passwd, NULL, NULL, NULL,
115
0
                               source, &creds_out);
116
0
  }
117
0
  else {
118
0
    result = Curl_creds_create(user ? user : Curl_creds_user(creds_in),
119
0
                               passwd ? passwd : Curl_creds_passwd(creds_in),
120
0
                               Curl_creds_oauth_bearer(creds_in),
121
0
                               Curl_creds_sasl_authzid(creds_in),
122
0
                               Curl_creds_sasl_service(creds_in),
123
0
                               source, &creds_out);
124
0
  }
125
0
  Curl_creds_link(pcreds_out, creds_out);
126
0
  Curl_creds_unlink(&creds_out);
127
0
  return result;
128
0
}
129
130
void Curl_creds_link(struct Curl_creds **pdest, struct Curl_creds *src)
131
0
{
132
0
  if(*pdest != src) {
133
0
    Curl_creds_unlink(pdest);
134
0
    *pdest = src;
135
0
    if(src) {
136
0
      DEBUGASSERT(src->refcount < UINT32_MAX);
137
0
      src->refcount++;
138
0
    }
139
0
  }
140
0
}
141
142
void Curl_creds_unlink(struct Curl_creds **pcreds)
143
23.7k
{
144
23.7k
  if(*pcreds) {
145
0
    struct Curl_creds *creds = *pcreds;
146
147
0
    DEBUGASSERT(creds->refcount);
148
0
    *pcreds = NULL;
149
0
    if(creds->refcount)
150
0
      creds->refcount--;
151
0
    if(!creds->refcount) {
152
0
      curlx_memzero(creds, sizeof(*creds) + creds->bufsize);
153
0
      curlx_free(creds);
154
0
    }
155
0
  }
156
23.7k
}
157
158
bool Curl_creds_same(struct Curl_creds *c1, struct Curl_creds *c2)
159
0
{
160
0
  return (c1 == c2) ||
161
0
         (c1 && c2 &&
162
0
          !Curl_timestrcmp(c1->user, c2->user) &&
163
0
          !Curl_timestrcmp(c1->passwd, c2->passwd) &&
164
0
          !Curl_timestrcmp(c1->oauth_bearer, c2->oauth_bearer) &&
165
0
          !Curl_timestrcmp(c1->sasl_authzid, c2->sasl_authzid) &&
166
0
          !Curl_timestrcmp(c1->sasl_service, c2->sasl_service));
167
0
}
168
169
bool Curl_creds_equal(struct Curl_creds *c1, struct Curl_creds *c2)
170
0
{
171
0
  return Curl_creds_same(c1, c2) &&
172
0
         ((c1 == c2) || (c1 && c2 && (c1->source == c2->source)));
173
0
}
174
175
#ifdef CURLVERBOSE
176
void Curl_creds_trace(struct Curl_easy *data, struct Curl_creds *creds,
177
                      const char *msg)
178
0
{
179
0
  if(creds) {
180
0
    CURL_TRC_M(data, "%s: user=%s, passwd=%s, "
181
0
               "sasl_authzid=%s, oauth_bearer=%s, source=%d",
182
0
               msg,
183
0
               Curl_creds_user(creds),
184
0
               Curl_creds_has_passwd(creds) ? "***" : "",
185
0
               Curl_creds_sasl_authzid(creds),
186
0
               Curl_creds_has_oauth_bearer(creds) ? "***" : "",
187
0
               creds->source);
188
0
  }
189
0
  else
190
0
    CURL_TRC_M(data, "%s: -", msg);
191
0
}
192
#endif