Get a password from the password file. Return value is malloc'd. */
| 7238 | |
| 7239 | /* Get a password from the password file. Return value is malloc'd. */ |
| 7240 | static char * |
| 7241 | passwordFromFile(const char *hostname, const char *port, const char *dbname, |
| 7242 | const char *username, const char *pgpassfile) |
| 7243 | { |
| 7244 | FILE *fp; |
| 7245 | struct stat stat_buf; |
| 7246 | PQExpBufferData buf; |
| 7247 | |
| 7248 | if (dbname == NULL || dbname[0] == '\0') |
| 7249 | return NULL; |
| 7250 | |
| 7251 | if (username == NULL || username[0] == '\0') |
| 7252 | return NULL; |
| 7253 | |
| 7254 | /* 'localhost' matches pghost of '' or the default socket directory */ |
| 7255 | if (hostname == NULL || hostname[0] == '\0') |
| 7256 | hostname = DefaultHost; |
| 7257 | else if (is_unixsock_path(hostname)) |
| 7258 | |
| 7259 | /* |
| 7260 | * We should probably use canonicalize_path(), but then we have to |
| 7261 | * bring path.c into libpq, and it doesn't seem worth it. |
| 7262 | */ |
| 7263 | if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0) |
| 7264 | hostname = DefaultHost; |
| 7265 | |
| 7266 | if (port == NULL || port[0] == '\0') |
| 7267 | port = DEF_PGPORT_STR; |
| 7268 | |
| 7269 | /* If password file cannot be opened, ignore it. */ |
| 7270 | if (stat(pgpassfile, &stat_buf) != 0) |
| 7271 | return NULL; |
| 7272 | |
| 7273 | #ifndef WIN32 |
| 7274 | if (!S_ISREG(stat_buf.st_mode)) |
| 7275 | { |
| 7276 | fprintf(stderr, |
| 7277 | libpq_gettext("WARNING: password file \"%s\" is not a plain file\n"), |
| 7278 | pgpassfile); |
| 7279 | return NULL; |
| 7280 | } |
| 7281 | |
| 7282 | /* If password file is insecure, alert the user and ignore it. */ |
| 7283 | if (stat_buf.st_mode & (S_IRWXG | S_IRWXO)) |
| 7284 | { |
| 7285 | fprintf(stderr, |
| 7286 | libpq_gettext("WARNING: password file \"%s\" has group or world access; permissions should be u=rw (0600) or less\n"), |
| 7287 | pgpassfile); |
| 7288 | return NULL; |
| 7289 | } |
| 7290 | #else |
| 7291 | |
| 7292 | /* |
| 7293 | * On Win32, the directory is protected, so we don't have to check the |
| 7294 | * file. |
| 7295 | */ |
| 7296 | #endif |
| 7297 |
no test coverage detected