I try on PostgreSQL 9.3 to get the lines of pg_hba.conf
where the authentication method is trust
from user with minimal privileges. I wrote a function, but it doesn't work:
create function public.get_pghba_with_trust()
returns TABLE (
TYPE text
, DATABASE text
, PGUSER text
, ADDRESS text
, METHOD text)
as
$$
SELECT *
FROM regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a)
WHERE a ~ '^[^#].*trust\s*$';
$$
language sql
security definer;
This returns an error:
ERROR: return type mismatch in function declared to return record
DETAIL: Final statement returns too few columns.
CONTEXT: SQL function "get_pghba_with_trust"
Grant execute on function
grant execute on function get_pghba_with_trust() to redcheck;
Try other function
CREATE OR REPLACE FUNCTION get_pghba_with_trust() RETURNS text AS $$
DECLARE
trust text;
BEGIN
SELECT * INTO trust from regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a) WHERE a ~ '^[^#].*trust\s*$';
IF NOT FOUND THEN
RETURN 'not used';
ELSE
RETURN trust;
END IF;
END;
$$ LANGUAGE plpgsql security definer;
Grant execute on function
grant execute on function get_pghba_with_trust() to redcheck;
Try run function get_pghba_with_trust
SELECT * FROM get_pghba_with_trust();
Error:
ERROR: must be superuser TO READ files
CONTEXT: SQL STATEMENT "SELECT * from regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a) WHERE a ~ '^[^#].*trust\s*$'" PL/pgSQL FUNCTION get_pghba_with_trust() line 5 AT SQL STATEMENT
How get lines of pg_hba.conf on PostgreSQL 9.3 with trust method from user with minimal privileges?
regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n')
returns a single line of text, you should split it again