The files libpq reads besides certificates: the password file (.pgpass) and the connection service file (pg_service.conf), plus the OS user their defaults come from. Both formats follow libpq's rules.
Depends only on pg_errors and nimcrypto's burnMem. Internal module: not part of the public API. Import the pg_connection hub instead.
Types
ConfFileFault = enum cffNone, ## Read. cffOpen, ## Could not be opened (missing, no access). cffStat, cffNotRegular, ## A directory, FIFO, device or the like. cffLoosePerms, ## Group or world permission bits (POSIX, when checked). cffTooLarge, ## Over `MaxConfFileBytes`. cffRead
- Why readConfFile read nothing.
PassFileLookup = object password*: string found*: bool note*: string ## Added to a 28P01 refusal: where the password came from, or why the ## file was skipped. Empty: nothing worth saying.
- What lookupPassFile found for one host.
ServiceEntry = object key*: string value*: string line*: int
- One key=value line of a service section.
Consts
MaxConfFileBytes = 4194304
- Cap for every configuration file read (DoS bound; cert chains fit comfortably under this).
Procs
proc confFileMissing(path: string): bool {....raises: [], tags: [], forbids: [].}
- Whether nothing is at path, which libpq's stat reports as ENOENT or ENOTDIR, rather than a file that cannot be read.
proc confFileStats(path: string): bool {....raises: [], tags: [], forbids: [].}
- Whether stat succeeds on path: libpq skips a service file, or an sslrootcert it need not verify with, that it cannot stat for any reason.
proc findPassword(content, host, port, database, user: string): tuple[ found: bool, password: string] {....raises: [], tags: [], forbids: [].}
- The first line of password file content matching all four keys. Scanned in place: only the password returned is copied out of content.
proc lookupPassFile(path, host, port, database, user: string): PassFileLookup {. ...raises: [], tags: [ReadIOEffect], forbids: [].}
- The password for one host from the password file at path, as libpq does. A file that is missing, unreadable or unsafe is skipped, never an error. host is the matching key (localhost for a Unix socket), database already defaulted to user.
proc osUserInfo(): tuple[name, home: string] {....raises: [], tags: [], forbids: [].}
- The effective user's name and, on POSIX, home directory from the user database, the fallback libpq uses. Empty when unknown.
proc parseServiceSection(content, path, service: string; found: var bool): seq[ ServiceEntry] {....raises: [PgConfigError], tags: [], forbids: [].}
-
The entries of section service in service file content, as libpq: the first section of that name wins, and within it the first value of a key. Unlike libpq, whitespace around = is trimmed, and lines have no length limit. Only the section's own lines are checked. Errors name the file and line, never a value.
Scanned in place, as findPassword: other sections' passwords are never copied out of content, so wiping it is enough.
proc readConfFile(path: string; content: var string; checkPerms = false): ConfFileFault {. ...raises: [], tags: [ReadIOEffect], forbids: [].}
- Read the regular file at path into content. With checkPerms, any group or world permission bit is a fault (POSIX only: libpq skips the check on Windows too). path must hold no NUL; the C open would cut it.
proc wipe(s: var string) {....raises: [], tags: [], forbids: [].}
- Zero s before it is dropped: a file's other lines (passwords, a key) need not linger in freed memory. burnMem's volatile memset is not elided as a dead store, which a plain loop before the free may be.