• Tom Lane's avatar
    Fix libpq's code for searching .pgpass; rationalize empty-list-item cases. · e3f99e03
    Tom Lane authored
    Before v10, we always searched ~/.pgpass using the host parameter,
    and nothing else, to match to the "hostname" field of ~/.pgpass.
    (However, null host or host matching DEFAULT_PGSOCKET_DIR was replaced by
    "localhost".)  In v10, this got broken by commit 274bb2b3, repaired by
    commit bdac9836, and broken again by commit 7b02ba62; in the code
    actually shipped, we'd search with hostaddr if both that and host were
    specified --- though oddly, *not* if only hostaddr were specified.
    Since this is directly contrary to the documentation, and not
    backwards-compatible, it's clearly a bug.
    
    However, the change wasn't totally without justification, even though it
    wasn't done quite right, because the pre-v10 behavior has arguably been
    buggy since we added hostaddr.  If hostaddr is specified and host isn't,
    the pre-v10 code will search ~/.pgpass for "localhost", and ship that
    password off to a server that most likely isn't local at all.  That's
    unhelpful at best, and could be a security breach at worst.
    
    Therefore, rather than just revert to that old behavior, let's define
    the behavior as "search with host if provided, else with hostaddr if
    provided, else search for localhost".  (As before, a host name matching
    DEFAULT_PGSOCKET_DIR is replaced by localhost.)  This matches the
    behavior of the actual connection code, so that we don't pick up an
    inappropriate password; and it allows useful searches to happen when
    only hostaddr is given.
    
    While we're messing around here, ensure that empty elements within a
    host or hostaddr list select the same behavior as a totally-empty
    field would; for instance "host=a,,b" is equivalent to "host=a,/tmp,b"
    if DEFAULT_PGSOCKET_DIR is /tmp.  Things worked that way in some cases
    already, but not consistently so, which contributed to the confusion
    about what key ~/.pgpass would get searched with.
    
    Update documentation accordingly, and also clarify some nearby text.
    
    Back-patch to v10 where the host/hostaddr list functionality was
    introduced.
    
    Discussion: https://postgr.es/m/30805.1532749137@sss.pgh.pa.us
    e3f99e03
fe-connect.c 163 KB