Coverage Report

Created: 2026-05-30 06:06

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
  CURLcode result = CURLE_OK;
50
51
0
  Curl_creds_unlink(pcreds);
52
53
  /* Everything empty/NULL, this is the NULL credential */
54
0
  if(!ulen && !plen && !olen && !salen && !sslen)
55
0
    goto out;
56
57
0
  if((ulen > CURL_MAX_INPUT_LENGTH) ||
58
0
     (plen > CURL_MAX_INPUT_LENGTH) ||
59
0
     (olen > CURL_MAX_INPUT_LENGTH) ||
60
0
     (salen > CURL_MAX_INPUT_LENGTH) ||
61
0
     (sslen > CURL_MAX_INPUT_LENGTH)) {
62
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
63
0
    goto out;
64
0
  }
65
66
  /* null-terminator for user already part of struct */
67
0
  creds = curlx_calloc(1, sizeof(*creds) +
68
0
                       ulen + plen + 1 + olen + 1 + salen + 1 + sslen + 1);
69
0
  if(!creds) {
70
0
    result = CURLE_OUT_OF_MEMORY;
71
0
    goto out;
72
0
  }
73
74
0
  creds->refcount = 1;
75
0
  creds->source = source;
76
  /* Some compilers try to be too smart about our dynamic struct size */
77
0
  buf = ((char *)creds) + offsetof(struct Curl_creds, buf);
78
0
  creds->user = s = buf;
79
0
  if(ulen)
80
0
    memcpy(s, user, ulen + 1);
81
0
  creds->passwd = s = buf + ulen + 1;
82
0
  if(plen)
83
0
    memcpy(s, passwd, plen + 1);
84
0
  creds->oauth_bearer = s = buf + ulen + 1 + plen + 1;
85
0
  if(olen)
86
0
    memcpy(s, oauth_bearer, olen + 1);
87
0
  creds->sasl_authzid = s = buf + ulen + 1 + plen + 1 + olen + 1;
88
0
  if(salen)
89
0
    memcpy(s, sasl_authzid, salen + 1);
90
0
  creds->sasl_service = s = buf + ulen + 1 + plen + 1 + olen + 1 + salen + 1;
91
0
  if(sslen)
92
0
    memcpy(s, sasl_service, sslen + 1);
93
94
0
out:
95
0
  if(!result)
96
0
    *pcreds = creds;
97
0
  else
98
0
    Curl_creds_unlink(&creds);
99
0
  return result;
100
0
}
101
102
CURLcode Curl_creds_merge(const char *user,
103
                          const char *passwd,
104
                          struct Curl_creds *creds_in,
105
                          uint8_t source,
106
                          struct Curl_creds **pcreds_out)
107
0
{
108
0
  struct Curl_creds *creds_out = NULL;
109
0
  CURLcode result;
110
111
0
  if(!user || !user[0])
112
0
    user = Curl_creds_user(creds_in);
113
0
  if(!passwd || !passwd[0])
114
0
    passwd = Curl_creds_passwd(creds_in);
115
0
  result = Curl_creds_create(user, passwd,
116
0
                             Curl_creds_oauth_bearer(creds_in),
117
0
                             Curl_creds_sasl_authzid(creds_in),
118
0
                             Curl_creds_sasl_service(creds_in),
119
0
                             source, &creds_out);
120
0
  Curl_creds_link(pcreds_out, creds_out);
121
0
  Curl_creds_unlink(&creds_out);
122
0
  return result;
123
0
}
124
125
void Curl_creds_link(struct Curl_creds **pdest, struct Curl_creds *src)
126
0
{
127
0
  if(*pdest != src) {
128
0
    Curl_creds_unlink(pdest);
129
0
    *pdest = src;
130
0
    if(src) {
131
0
      DEBUGASSERT(src->refcount < UINT32_MAX);
132
0
      src->refcount++;
133
0
    }
134
0
  }
135
0
}
136
137
void Curl_creds_unlink(struct Curl_creds **pcreds)
138
0
{
139
0
  if(*pcreds) {
140
0
    struct Curl_creds *creds = *pcreds;
141
142
0
    DEBUGASSERT(creds->refcount);
143
0
    *pcreds = NULL;
144
0
    if(creds->refcount)
145
0
      creds->refcount--;
146
0
    if(!creds->refcount) {
147
0
      curlx_free(creds);
148
0
    }
149
0
  }
150
0
}
151
152
bool Curl_creds_same(struct Curl_creds *c1, struct Curl_creds *c2)
153
0
{
154
0
  return (c1 == c2) ||
155
0
         (c1 && c2 &&
156
0
          !Curl_timestrcmp(c1->user, c2->user) &&
157
0
          !Curl_timestrcmp(c1->passwd, c2->passwd) &&
158
0
          !Curl_timestrcmp(c1->oauth_bearer, c2->oauth_bearer) &&
159
0
          !Curl_timestrcmp(c1->sasl_authzid, c2->sasl_authzid) &&
160
0
          !Curl_timestrcmp(c1->sasl_service, c2->sasl_service));
161
0
}
162
163
#ifdef CURLVERBOSE
164
void Curl_creds_trace(struct Curl_easy *data, struct Curl_creds *creds,
165
                      const char *msg)
166
0
{
167
0
  if(creds) {
168
0
    CURL_TRC_M(data, "%s: user=%s, passwd=%s, "
169
0
               "sasl_authzid=%s, oauth_bearer=%s, source=%d",
170
0
               msg,
171
0
               Curl_creds_user(creds),
172
0
               Curl_creds_has_passwd(creds) ? "***" : "",
173
0
               Curl_creds_sasl_authzid(creds),
174
0
               Curl_creds_has_oauth_bearer(creds) ? "***" : "",
175
0
               creds->source);
176
0
  }
177
0
  else
178
0
    CURL_TRC_M(data, "%s: -", msg);
179
0
}
180
#endif