Can _netrc handle passphrases with spaces?
Asked Answered
A

4

10

The following configuration works:

machine code.mycompany.net
login supernerd
password HelloW0rld

The following configuration doesn't work:

machine code.mycompany.net
login supernerd
password Please excuse my dear aunt sally.
Albina answered 1/10, 2012 at 14:7 Comment(3)
Put quotes around the password.Directions
@Directions And how to store a password with a space and a quote?Brooch
@Brooch the only problem would be if the password had both single and double quotes.Directions
T
5

From this bug report (now python/cpython issue 36615) or this page, spaces in password don't seem to be supported in a .netrc (or _netrc) file.
Or even if they are, not all the programs using that .netrc file will be able to interpret said space correctly.


As runrig mentions in the comments:

Quoting the field as in another answer here should work, but the python library doesn't like it.
But, e.g., command line ftp and the perl netrc library is fine with it.

So this should work when used when those commands:

password "Please excuse my dear aunt sally."
Tusker answered 1/10, 2012 at 14:25 Comment(4)
The man page doesn't mention spaces when used that way in a netrc file: linux.die.net/man/5/netrcTusker
The python netrc library doesn't support spaces, but should. It's a bug in the library. Quoting the field as in another answer here should work, but the python library doesn't like it. But, e.g., command line ftp and the perl netrc library is fine with it.Directions
@Directions do you know if this issue is already resolved or addresed in ticket ? As I understand it is a bug in urllib ?Coauthor
@Coauthor I have no idea. It's been a while since I've needed to do this in python.Directions
R
4

Using ftp on IRIX 6.5 running on an SGI, I added quotes around my password and it works fine, e.g.:

password "Please excuse my dear aunt sally." 
Rabble answered 1/11, 2012 at 16:12 Comment(1)
git (v1.9.5) does not understand quoted passwords containing spaces.Buttermilk
B
1

This is the tokenizer of the GNU Inetutils version 2.5:

token (void)
{
  char *cp;
  int c;
  struct toktab *t;

  if (feof (cfile) || ferror (cfile))
    return (0);
  while ((c = getc (cfile)) != EOF &&
         (c == '\n' || c == '\t' || c == ' ' || c == ','))
    continue;
  if (c == EOF)
    return (0);
  cp = tokval;
  if (c == '"')
    {
      while ((c = getc (cfile)) != EOF && c != '"')
        {
          if (c == '\\')
            c = getc (cfile);
          *cp++ = c;
        }
    }
  else
    {
      *cp++ = c;
      while ((c = getc (cfile)) != EOF
             && c != '\n' && c != '\t' && c != ' ' && c != ',')
        {
          if (c == '\\')
            c = getc (cfile);
          *cp++ = c;
        }
    }
  *cp = 0;
  if (tokval[0] == 0)
    return (0);
  for (t = toktab; t->tokstr; t++)
    if (!strcmp (t->tokstr, tokval))
      return (t->tval);
  return (ID);
}

As you can see: tokens may be surrounded by double quotes.

  if (c == '"')
    {
      while ((c = getc (cfile)) != EOF && c != '"')
        {
          if (c == '\\')
            c = getc (cfile);
          *cp++ = c;
        }
    }
Brooch answered 11/3 at 15:7 Comment(0)
M
0

I have studied wget's netrc.c; I hope most netrc parsers work similarly:

Note that # after whitespace starts a comment until end of line.

A string can be put between double quotes (for example if the string starts with #, or contains a lot of whitespace that we don't want to backslash).

"#pass%"
"S3cret with spaces"

To make sure a character is understood as part of a string, prefix it with a backslash so that it is understood verbatim. This is supported with or without double quotes:

S3cret\ with\ spaces
\#not-a-comment
backslash:\\
"anything"
"double\"quote"
Maharajah answered 1/10, 2012 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.