netrc: use the NETRC environment variable (first) if set

Add test 755 to verify.

Proposed-by: Berthin Torres Callañaupa
URL: https://curl.se/mail/lib-2025-06/0015.html

Closes #17712
This commit is contained in:
Daniel Stenberg 2025-06-23 00:09:18 +02:00
parent cb9b1a4c4e
commit cb2a48bf5b
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 111 additions and 39 deletions

View file

@ -23,18 +23,21 @@ and password. This is typically used for FTP on Unix. If used with HTTP, curl
enables user authentication. See *netrc(5)* and *ftp(1)* for details on the
file format. curl does not complain if that file does not have the right
permissions (it should be neither world- nor group-readable). The environment
variable "HOME" is used to find the home directory.
variable `HOME` is used to find the home directory. If the `NETRC` environment
variable is set, that filename is used as the netrc file. (Added in 8.16.0)
If --netrc-file is used, that overrides all other ways to figure out the file.
The netrc file provides credentials for a hostname independent of which
protocol and port number that are used.
On Windows two filenames in the home directory are checked: *.netrc* and
*_netrc*, preferring the former. Older versions on Windows checked for *_netrc*
only.
*_netrc*, preferring the former. Older versions on Windows checked for
*_netrc* only.
A quick and simple example of how to setup a *.netrc* to allow curl to FTP to
the machine host.example.com with username 'myself' and password 'secret' could
look similar to:
the machine host.example.com with username 'myself' and password 'secret'
could look similar to:
machine host.example.com
login myself

View file

@ -66,6 +66,11 @@ When the netrc feature is used (CURLOPT_NETRC(3)), this variable is
checked as the secondary way to find the "current" home directory (on Windows
only) in which the .netrc file is likely to exist.
## `NETRC`
The filename used as netrc file when CURLOPT_NETRC(3) is used without
CURLOPT_NETRC_FILE(3). (Added in 8.16.0)
## `NO_PROXY`
This has the same functionality as the CURLOPT_NOPROXY(3) option: it

View file

@ -29,7 +29,8 @@ CURLcode curl_easy_setopt(CURL *handle, CURLOPT_NETRC, long level);
This parameter controls the preference *level* of libcurl between using
usernames and passwords from your *~/.netrc* file, relative to usernames and
passwords in the URL supplied with CURLOPT_URL(3).
passwords in the URL supplied with CURLOPT_URL(3). If the `NETRC` environment
variable is set, that filename is used as the netrc file. (Added in 8.16.0)
On Windows, libcurl primarily checks for *.netrc* in *%HOME%*. If *%HOME%* is
not set on Windows, libcurl falls back to *%USERPROFILE%*. If the file does