Coverage Report

Created: 2025-06-09 07:43

/src/gdal/curl/lib/netrc.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
#ifndef CURL_DISABLE_NETRC
27
28
#ifdef HAVE_PWD_H
29
#ifdef __AMIGA__
30
#undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */
31
#endif
32
#include <pwd.h>
33
#ifdef __AMIGA__
34
#define __NO_NET_API
35
#endif
36
#endif
37
38
#include <curl/curl.h>
39
#include "netrc.h"
40
#include "strcase.h"
41
#include "curl_get_line.h"
42
#include "curlx/strparse.h"
43
44
/* The last 3 #include files should be in this order */
45
#include "curl_printf.h"
46
#include "curl_memory.h"
47
#include "memdebug.h"
48
49
/* Get user and password from .netrc when given a machine name */
50
51
enum host_lookup_state {
52
  NOTHING,
53
  HOSTFOUND,    /* the 'machine' keyword was found */
54
  HOSTVALID,    /* this is "our" machine! */
55
  MACDEF
56
};
57
58
enum found_state {
59
  NONE,
60
  LOGIN,
61
  PASSWORD
62
};
63
64
0
#define FOUND_LOGIN    1
65
0
#define FOUND_PASSWORD 2
66
67
11.9k
#define MAX_NETRC_LINE 16384
68
79.0k
#define MAX_NETRC_FILE (128*1024)
69
11.9k
#define MAX_NETRC_TOKEN 4096
70
71
/* convert a dynbuf call CURLcode error to a NETRCcode error */
72
#define curl2netrc(result)                     \
73
0
  (((result) == CURLE_OUT_OF_MEMORY) ?         \
74
0
   NETRC_OUT_OF_MEMORY : NETRC_SYNTAX_ERROR)
75
76
static NETRCcode file2memory(const char *filename, struct dynbuf *filebuf)
77
11.9k
{
78
11.9k
  NETRCcode ret = NETRC_FILE_MISSING; /* if it cannot open the file */
79
11.9k
  FILE *file = fopen(filename, FOPEN_READTEXT);
80
11.9k
  struct dynbuf linebuf;
81
11.9k
  curlx_dyn_init(&linebuf, MAX_NETRC_LINE);
82
83
11.9k
  if(file) {
84
0
    ret = NETRC_OK;
85
0
    while(Curl_get_line(&linebuf, file)) {
86
0
      CURLcode result;
87
0
      const char *line = curlx_dyn_ptr(&linebuf);
88
      /* skip comments on load */
89
0
      curlx_str_passblanks(&line);
90
0
      if(*line == '#')
91
0
        continue;
92
0
      result = curlx_dyn_add(filebuf, line);
93
0
      if(result) {
94
0
        ret = curl2netrc(result);
95
0
        goto done;
96
0
      }
97
0
    }
98
0
  }
99
11.9k
done:
100
11.9k
  curlx_dyn_free(&linebuf);
101
11.9k
  if(file)
102
0
    fclose(file);
103
11.9k
  return ret;
104
11.9k
}
105
106
/*
107
 * Returns zero on success.
108
 */
109
static NETRCcode parsenetrc(struct store_netrc *store,
110
                            const char *host,
111
                            char **loginp, /* might point to a username */
112
                            char **passwordp,
113
                            const char *netrcfile)
114
11.9k
{
115
11.9k
  NETRCcode retcode = NETRC_NO_MATCH;
116
11.9k
  char *login = *loginp;
117
11.9k
  char *password = NULL;
118
11.9k
  bool specific_login = !!login; /* points to something */
119
11.9k
  enum host_lookup_state state = NOTHING;
120
11.9k
  enum found_state keyword = NONE;
121
11.9k
  unsigned char found = 0; /* login + password found bits, as they can come in
122
                              any order */
123
11.9k
  bool our_login = FALSE;  /* found our login name */
124
11.9k
  bool done = FALSE;
125
11.9k
  char *netrcbuffer;
126
11.9k
  struct dynbuf token;
127
11.9k
  struct dynbuf *filebuf = &store->filebuf;
128
11.9k
  DEBUGASSERT(!*passwordp);
129
11.9k
  curlx_dyn_init(&token, MAX_NETRC_TOKEN);
130
131
11.9k
  if(!store->loaded) {
132
11.9k
    NETRCcode ret = file2memory(netrcfile, filebuf);
133
11.9k
    if(ret)
134
11.9k
      return ret;
135
0
    store->loaded = TRUE;
136
0
  }
137
138
0
  netrcbuffer = curlx_dyn_ptr(filebuf);
139
140
0
  while(!done) {
141
0
    const char *tok = netrcbuffer;
142
0
    while(tok && !done) {
143
0
      const char *tok_end;
144
0
      bool quoted;
145
0
      curlx_dyn_reset(&token);
146
0
      curlx_str_passblanks(&tok);
147
      /* tok is first non-space letter */
148
0
      if(state == MACDEF) {
149
0
        if((*tok == '\n') || (*tok == '\r'))
150
0
          state = NOTHING; /* end of macro definition */
151
0
      }
152
153
0
      if(!*tok || (*tok == '\n'))
154
        /* end of line  */
155
0
        break;
156
157
      /* leading double-quote means quoted string */
158
0
      quoted = (*tok == '\"');
159
160
0
      tok_end = tok;
161
0
      if(!quoted) {
162
0
        size_t len = 0;
163
0
        CURLcode result;
164
0
        while(*tok_end > ' ') {
165
0
          tok_end++;
166
0
          len++;
167
0
        }
168
0
        if(!len) {
169
0
          retcode = NETRC_SYNTAX_ERROR;
170
0
          goto out;
171
0
        }
172
0
        result = curlx_dyn_addn(&token, tok, len);
173
0
        if(result) {
174
0
          retcode = curl2netrc(result);
175
0
          goto out;
176
0
        }
177
0
      }
178
0
      else {
179
0
        bool escape = FALSE;
180
0
        bool endquote = FALSE;
181
0
        tok_end++; /* pass the leading quote */
182
0
        while(*tok_end) {
183
0
          CURLcode result;
184
0
          char s = *tok_end;
185
0
          if(escape) {
186
0
            escape = FALSE;
187
0
            switch(s) {
188
0
            case 'n':
189
0
              s = '\n';
190
0
              break;
191
0
            case 'r':
192
0
              s = '\r';
193
0
              break;
194
0
            case 't':
195
0
              s = '\t';
196
0
              break;
197
0
            }
198
0
          }
199
0
          else if(s == '\\') {
200
0
            escape = TRUE;
201
0
            tok_end++;
202
0
            continue;
203
0
          }
204
0
          else if(s == '\"') {
205
0
            tok_end++; /* pass the ending quote */
206
0
            endquote = TRUE;
207
0
            break;
208
0
          }
209
0
          result = curlx_dyn_addn(&token, &s, 1);
210
0
          if(result) {
211
0
            retcode = curl2netrc(result);
212
0
            goto out;
213
0
          }
214
0
          tok_end++;
215
0
        }
216
0
        if(escape || !endquote) {
217
          /* bad syntax, get out */
218
0
          retcode = NETRC_SYNTAX_ERROR;
219
0
          goto out;
220
0
        }
221
0
      }
222
223
0
      if(curlx_dyn_len(&token))
224
0
        tok = curlx_dyn_ptr(&token);
225
0
      else
226
        /* since tok might actually be NULL for no content, set it to blank
227
           to avoid having to deal with it being NULL */
228
0
        tok = "";
229
230
0
      switch(state) {
231
0
      case NOTHING:
232
0
        if(strcasecompare("macdef", tok))
233
          /* Define a macro. A macro is defined with the specified name; its
234
             contents begin with the next .netrc line and continue until a
235
             null line (consecutive new-line characters) is encountered. */
236
0
          state = MACDEF;
237
0
        else if(strcasecompare("machine", tok)) {
238
          /* the next tok is the machine name, this is in itself the delimiter
239
             that starts the stuff entered for this machine, after this we
240
             need to search for 'login' and 'password'. */
241
0
          state = HOSTFOUND;
242
0
          keyword = NONE;
243
0
          found = 0;
244
0
          our_login = FALSE;
245
0
          Curl_safefree(password);
246
0
          if(!specific_login)
247
0
            Curl_safefree(login);
248
0
        }
249
0
        else if(strcasecompare("default", tok)) {
250
0
          state = HOSTVALID;
251
0
          retcode = NETRC_OK; /* we did find our host */
252
0
        }
253
0
        break;
254
0
      case MACDEF:
255
0
        if(!*tok)
256
0
          state = NOTHING;
257
0
        break;
258
0
      case HOSTFOUND:
259
0
        if(strcasecompare(host, tok)) {
260
          /* and yes, this is our host! */
261
0
          state = HOSTVALID;
262
0
          retcode = NETRC_OK; /* we did find our host */
263
0
        }
264
0
        else
265
          /* not our host */
266
0
          state = NOTHING;
267
0
        break;
268
0
      case HOSTVALID:
269
        /* we are now parsing sub-keywords concerning "our" host */
270
0
        if(keyword == LOGIN) {
271
0
          if(specific_login)
272
0
            our_login = !Curl_timestrcmp(login, tok);
273
0
          else {
274
0
            our_login = TRUE;
275
0
            free(login);
276
0
            login = strdup(tok);
277
0
            if(!login) {
278
0
              retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */
279
0
              goto out;
280
0
            }
281
0
          }
282
0
          found |= FOUND_LOGIN;
283
0
          keyword = NONE;
284
0
        }
285
0
        else if(keyword == PASSWORD) {
286
0
          free(password);
287
0
          password = strdup(tok);
288
0
          if(!password) {
289
0
            retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */
290
0
            goto out;
291
0
          }
292
0
          if(!specific_login || our_login)
293
0
            found |= FOUND_PASSWORD;
294
0
          keyword = NONE;
295
0
        }
296
0
        else if(strcasecompare("login", tok))
297
0
          keyword = LOGIN;
298
0
        else if(strcasecompare("password", tok))
299
0
          keyword = PASSWORD;
300
0
        else if(strcasecompare("machine", tok)) {
301
          /* a new machine here */
302
0
          if(found & FOUND_PASSWORD) {
303
0
            done = TRUE;
304
0
            break;
305
0
          }
306
0
          state = HOSTFOUND;
307
0
          keyword = NONE;
308
0
          found = 0;
309
0
          Curl_safefree(password);
310
0
          if(!specific_login)
311
0
            Curl_safefree(login);
312
0
        }
313
0
        else if(strcasecompare("default", tok)) {
314
0
          state = HOSTVALID;
315
0
          retcode = NETRC_OK; /* we did find our host */
316
0
          Curl_safefree(password);
317
0
          if(!specific_login)
318
0
            Curl_safefree(login);
319
0
        }
320
0
        if((found == (FOUND_PASSWORD|FOUND_LOGIN)) && our_login) {
321
0
          done = TRUE;
322
0
          break;
323
0
        }
324
0
        break;
325
0
      } /* switch (state) */
326
0
      tok = ++tok_end;
327
0
    }
328
0
    if(!done) {
329
0
      char *nl = NULL;
330
0
      if(tok)
331
0
        nl = strchr(tok, '\n');
332
0
      if(!nl)
333
0
        break;
334
      /* point to next line */
335
0
      netrcbuffer = &nl[1];
336
0
    }
337
0
  } /* while !done */
338
339
0
out:
340
0
  curlx_dyn_free(&token);
341
0
  if(!retcode) {
342
0
    if(!password && our_login) {
343
      /* success without a password, set a blank one */
344
0
      password = strdup("");
345
0
      if(!password)
346
0
        retcode = NETRC_OUT_OF_MEMORY; /* out of memory */
347
0
    }
348
0
    else if(!login && !password)
349
      /* a default with no credentials */
350
0
      retcode = NETRC_NO_MATCH;
351
0
  }
352
0
  if(!retcode) {
353
    /* success */
354
0
    if(!specific_login)
355
0
      *loginp = login;
356
0
    *passwordp = password;
357
0
  }
358
0
  else {
359
0
    curlx_dyn_free(filebuf);
360
0
    if(!specific_login)
361
0
      free(login);
362
0
    free(password);
363
0
  }
364
365
0
  return retcode;
366
0
}
367
368
const char *Curl_netrc_strerror(NETRCcode ret)
369
0
{
370
0
  switch(ret) {
371
0
  default:
372
0
    return ""; /* not a legit error */
373
0
  case NETRC_FILE_MISSING:
374
0
    return "no such file";
375
0
  case NETRC_NO_MATCH:
376
0
    return "no matching entry";
377
0
  case NETRC_OUT_OF_MEMORY:
378
0
    return "out of memory";
379
0
  case NETRC_SYNTAX_ERROR:
380
0
    return "syntax error";
381
0
  }
382
  /* never reached */
383
0
}
384
385
/*
386
 * @unittest: 1304
387
 *
388
 * *loginp and *passwordp MUST be allocated if they are not NULL when passed
389
 * in.
390
 */
391
NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host,
392
                          char **loginp, char **passwordp,
393
                          char *netrcfile)
394
11.9k
{
395
11.9k
  NETRCcode retcode = NETRC_OK;
396
11.9k
  char *filealloc = NULL;
397
398
11.9k
  if(!netrcfile) {
399
11.9k
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
400
11.9k
    char pwbuf[1024];
401
11.9k
#endif
402
11.9k
    char *home = NULL;
403
11.9k
    char *homea = curl_getenv("HOME"); /* portable environment reader */
404
11.9k
    if(homea) {
405
11.9k
      home = homea;
406
11.9k
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
407
11.9k
    }
408
0
    else {
409
0
      struct passwd pw, *pw_res;
410
0
      if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
411
0
         && pw_res) {
412
0
        home = pw.pw_dir;
413
0
      }
414
#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
415
    }
416
    else {
417
      struct passwd *pw;
418
      pw = getpwuid(geteuid());
419
      if(pw) {
420
        home = pw->pw_dir;
421
      }
422
#elif defined(_WIN32)
423
    }
424
    else {
425
      homea = curl_getenv("USERPROFILE");
426
      if(homea) {
427
        home = homea;
428
      }
429
#endif
430
0
    }
431
432
11.9k
    if(!home)
433
0
      return NETRC_FILE_MISSING; /* no home directory found (or possibly out
434
                                    of memory) */
435
436
11.9k
    filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
437
11.9k
    if(!filealloc) {
438
0
      free(homea);
439
0
      return NETRC_OUT_OF_MEMORY;
440
0
    }
441
11.9k
    retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
442
11.9k
    free(filealloc);
443
#ifdef _WIN32
444
    if(retcode == NETRC_FILE_MISSING) {
445
      /* fallback to the old-style "_netrc" file */
446
      filealloc = aprintf("%s%s_netrc", home, DIR_CHAR);
447
      if(!filealloc) {
448
        free(homea);
449
        return NETRC_OUT_OF_MEMORY;
450
      }
451
      retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
452
      free(filealloc);
453
    }
454
#endif
455
11.9k
    free(homea);
456
11.9k
  }
457
0
  else
458
0
    retcode = parsenetrc(store, host, loginp, passwordp, netrcfile);
459
11.9k
  return retcode;
460
11.9k
}
461
462
void Curl_netrc_init(struct store_netrc *s)
463
79.0k
{
464
79.0k
  curlx_dyn_init(&s->filebuf, MAX_NETRC_FILE);
465
79.0k
  s->loaded = FALSE;
466
79.0k
}
467
void Curl_netrc_cleanup(struct store_netrc *s)
468
81.3k
{
469
81.3k
  curlx_dyn_free(&s->filebuf);
470
81.3k
  s->loaded = FALSE;
471
81.3k
}
472
#endif