]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
netrc: use the NETRC environment variable (first) if set
authorDaniel Stenberg <daniel@haxx.se>
Sun, 22 Jun 2025 22:09:18 +0000 (00:09 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 27 Jul 2025 16:26:43 +0000 (18:26 +0200)
Add test 755 to verify.

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

Closes #17712

docs/cmdline-opts/netrc.md
docs/libcurl/libcurl-env.md
docs/libcurl/opts/CURLOPT_NETRC.md
lib/netrc.c
tests/data/Makefile.am
tests/data/test755 [new file with mode: 0644]

index 261dc196d6e84c7632b8a466287ac81c5f10c3aa..72b5ff935ba974b5fefe82e2ad8a0f3b02536246 100644 (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
index 3b9a83861fdd50ef06ed9c32e241add8869969b2..6ef11ac9a3ddf6fa7110df2be7d90992068a5fb4 100644 (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
index fa41236873549219c1f76fb3d3e4cc527c493598..bda7a66c398943e6cb3613e4735859a39f07430d 100644 (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
index bf53fc01e180323dc3ed6c583597c88bdf6b25a6..ab068b18e6e26a04734eed07f6be0d1563561838 100644 (file)
@@ -396,47 +396,51 @@ NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host,
   char *filealloc = NULL;
 
   if(!netrcfile) {
+    char *home = NULL;
+    char *homea = NULL;
 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
     char pwbuf[1024];
 #endif
-    char *home = NULL;
-    char *homea = curl_getenv("HOME"); /* portable environment reader */
-    if(homea) {
-      home = homea;
+    filealloc = curl_getenv("NETRC");
+    if(!filealloc) {
+      homea = curl_getenv("HOME"); /* portable environment reader */
+      if(homea) {
+        home = homea;
 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
-    }
-    else {
-      struct passwd pw, *pw_res;
-      if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
-         && pw_res) {
-        home = pw.pw_dir;
       }
+      else {
+        struct passwd pw, *pw_res;
+        if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
+           && pw_res) {
+          home = pw.pw_dir;
+        }
 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
-    }
-    else {
-      struct passwd *pw;
-      pw = getpwuid(geteuid());
-      if(pw) {
-        home = pw->pw_dir;
       }
+      else {
+        struct passwd *pw;
+        pw = getpwuid(geteuid());
+        if(pw) {
+          home = pw->pw_dir;
+        }
 #elif defined(_WIN32)
-    }
-    else {
-      homea = curl_getenv("USERPROFILE");
-      if(homea) {
-        home = homea;
       }
+      else {
+        homea = curl_getenv("USERPROFILE");
+        if(homea) {
+          home = homea;
+        }
 #endif
-    }
+      }
 
-    if(!home)
-      return NETRC_FILE_MISSING; /* no home directory found (or possibly out
-                                    of memory) */
+      if(!home)
+        return NETRC_FILE_MISSING; /* no home directory found (or possibly out
+                                      of memory) */
 
-    filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
-    if(!filealloc) {
-      free(homea);
-      return NETRC_OUT_OF_MEMORY;
+      filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
+      if(!filealloc) {
+        free(homea);
+        return NETRC_OUT_OF_MEMORY;
+      }
     }
     retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
     free(filealloc);
index 7d8e8c7fc5311ef7b722fbe7115de00ec5eef8da..d8d0ec5db582e4cb1148913af578e03a176f75b1 100644 (file)
@@ -108,7 +108,7 @@ test718 test719 test720 test721 test722 test723 test724 test725 test726 \
 test727 test728 test729 test730 test731 test732 test733 test734 test735 \
 test736 test737 test738 test739 test740 test741 test742 test743 test744 \
 test745 test746 test747 test748 test749 test750 test751 test752 test753 \
-test754 \
+test754 test755 \
 test780 test781 test782 test783 test784 test785 test786 test787 test788 \
 test789 test790 test791 test792 test793 \
 \
diff --git a/tests/data/test755 b/tests/data/test755
new file mode 100644 (file)
index 0000000..0e0840b
--- /dev/null
@@ -0,0 +1,59 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+netrc
+</keywords>
+</info>
+
+#
+# Server-side
+<reply>
+<data>
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Server: test-server/fake swsclose
+Content-Type: text/html
+Funny-head: yesyes
+Content-Length: 9
+
+contents
+</data>
+</reply>
+
+#
+# Client-side
+<client>
+<server>
+http
+</server>
+<name>
+netrc with NETRC pointing out the file
+</name>
+<file name="%LOGDIR/netrc%TESTNUMBER">
+machine foo.host login foo password alone-in-the-dark
+</file>
+<setenv>
+NETRC=%LOGDIR/netrc%TESTNUMBER
+</setenv>
+<command>
+http://foo.host/%TESTNUMBER --proxy %HOSTIP:%HTTPPORT -A "" --netrc
+</command>
+<features>
+proxy
+</features>
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+<protocol crlf="yes">
+GET http://foo.host/%TESTNUMBER HTTP/1.1
+Host: foo.host
+Authorization: Basic %b64[foo:alone-in-the-dark]b64%
+Accept: */*
+Proxy-Connection: Keep-Alive
+
+</protocol>
+</verify>
+</testcase>