Skip to content

Commit c591300

Browse files
committed
Add rule_number to pg_hba_file_rules and map_number to pg_ident_file_mappings
These numbers are strictly-monotone identifiers assigned to each rule of pg_hba_file_rules and each map of pg_ident_file_mappings when loading the HBA and ident configuration files, indicating the order in which they are checked at authentication time, until a match is found. With only one file loaded currently, this is equivalent to the line numbers assigned to the entries loaded if one wants to know their order, but this becomes mandatory once the inclusion of external files is added to the HBA and ident files to be able to know in which order the rules and/or maps are applied at authentication. Note that NULL is used when a HBA or ident entry cannot be parsed or validated, aka when an error exists, contrary to the line number. Bump catalog version. Author: Julien Rouhaud Discussion: https://postgr.es/m/20220223045959.35ipdsvbxcstrhya@jrouhaud
1 parent 37d2644 commit c591300

File tree

5 files changed

+74
-21
lines changed

5 files changed

+74
-21
lines changed

doc/src/sgml/system-views.sgml

+21
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,17 @@
991991
</thead>
992992

993993
<tbody>
994+
<row>
995+
<entry role="catalog_table_entry"><para role="column_definition">
996+
<structfield>rule_number</structfield> <type>int4</type>
997+
</para>
998+
<para>
999+
Number of this rule, if valid, otherwise <literal>NULL</literal>.
1000+
This indicates the order in which each rule is considered
1001+
until a match is found during authentication.
1002+
</para></entry>
1003+
</row>
1004+
9941005
<row>
9951006
<entry role="catalog_table_entry"><para role="column_definition">
9961007
<structfield>line_number</structfield> <type>int4</type>
@@ -1131,6 +1142,16 @@
11311142
</thead>
11321143

11331144
<tbody>
1145+
<row>
1146+
<entry role="catalog_table_entry"><para role="column_definition">
1147+
<structfield>map_number</structfield> <type>int4</type>
1148+
</para>
1149+
<para>
1150+
Number of this map, in priority order, if valid, otherwise
1151+
<literal>NULL</literal>
1152+
</para></entry>
1153+
</row>
1154+
11341155
<row>
11351156
<entry role="catalog_table_entry"><para role="column_definition">
11361157
<structfield>line_number</structfield> <type>int4</type>

src/backend/utils/adt/hbafuncs.c

+40-11
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626

2727
static ArrayType *get_hba_options(HbaLine *hba);
2828
static void fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
29-
int lineno, HbaLine *hba, const char *err_msg);
29+
int rule_number, int lineno, HbaLine *hba,
30+
const char *err_msg);
3031
static void fill_hba_view(Tuplestorestate *tuple_store, TupleDesc tupdesc);
3132
static void fill_ident_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
32-
int lineno, IdentLine *ident, const char *err_msg);
33+
int map_number, int lineno, IdentLine *ident,
34+
const char *err_msg);
3335
static void fill_ident_view(Tuplestorestate *tuple_store, TupleDesc tupdesc);
3436

3537

@@ -157,14 +159,15 @@ get_hba_options(HbaLine *hba)
157159
}
158160

159161
/* Number of columns in pg_hba_file_rules view */
160-
#define NUM_PG_HBA_FILE_RULES_ATTS 9
162+
#define NUM_PG_HBA_FILE_RULES_ATTS 10
161163

162164
/*
163165
* fill_hba_line
164166
* Build one row of pg_hba_file_rules view, add it to tuplestore.
165167
*
166168
* tuple_store: where to store data
167169
* tupdesc: tuple descriptor for the view
170+
* rule_number: unique identifier among all valid rules
168171
* lineno: pg_hba.conf line number (must always be valid)
169172
* hba: parsed line data (can be NULL, in which case err_msg should be set)
170173
* err_msg: error message (NULL if none)
@@ -174,7 +177,8 @@ get_hba_options(HbaLine *hba)
174177
*/
175178
static void
176179
fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
177-
int lineno, HbaLine *hba, const char *err_msg)
180+
int rule_number, int lineno, HbaLine *hba,
181+
const char *err_msg)
178182
{
179183
Datum values[NUM_PG_HBA_FILE_RULES_ATTS];
180184
bool nulls[NUM_PG_HBA_FILE_RULES_ATTS];
@@ -193,6 +197,12 @@ fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
193197
memset(nulls, 0, sizeof(nulls));
194198
index = 0;
195199

200+
/* rule_number, nothing on error */
201+
if (err_msg)
202+
nulls[index++] = true;
203+
else
204+
values[index++] = Int32GetDatum(rule_number);
205+
196206
/* line_number */
197207
values[index++] = Int32GetDatum(lineno);
198208

@@ -336,7 +346,7 @@ fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
336346
else
337347
{
338348
/* no parsing result, so set relevant fields to nulls */
339-
memset(&nulls[1], true, (NUM_PG_HBA_FILE_RULES_ATTS - 2) * sizeof(bool));
349+
memset(&nulls[2], true, (NUM_PG_HBA_FILE_RULES_ATTS - 3) * sizeof(bool));
340350
}
341351

342352
/* error */
@@ -359,6 +369,7 @@ fill_hba_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
359369
FILE *file;
360370
List *hba_lines = NIL;
361371
ListCell *line;
372+
int rule_number = 0;
362373
MemoryContext linecxt;
363374
MemoryContext hbacxt;
364375
MemoryContext oldcxt;
@@ -393,8 +404,12 @@ fill_hba_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
393404
if (tok_line->err_msg == NULL)
394405
hbaline = parse_hba_line(tok_line, DEBUG3);
395406

396-
fill_hba_line(tuple_store, tupdesc, tok_line->line_num,
397-
hbaline, tok_line->err_msg);
407+
/* No error, set a new rule number */
408+
if (tok_line->err_msg == NULL)
409+
rule_number++;
410+
411+
fill_hba_line(tuple_store, tupdesc, rule_number,
412+
tok_line->line_num, hbaline, tok_line->err_msg);
398413
}
399414

400415
/* Free tokenizer memory */
@@ -431,14 +446,15 @@ pg_hba_file_rules(PG_FUNCTION_ARGS)
431446
}
432447

433448
/* Number of columns in pg_ident_file_mappings view */
434-
#define NUM_PG_IDENT_FILE_MAPPINGS_ATTS 5
449+
#define NUM_PG_IDENT_FILE_MAPPINGS_ATTS 6
435450

436451
/*
437452
* fill_ident_line: build one row of pg_ident_file_mappings view, add it to
438453
* tuplestore
439454
*
440455
* tuple_store: where to store data
441456
* tupdesc: tuple descriptor for the view
457+
* map_number: unique identifier among all valid maps
442458
* lineno: pg_ident.conf line number (must always be valid)
443459
* ident: parsed line data (can be NULL, in which case err_msg should be set)
444460
* err_msg: error message (NULL if none)
@@ -448,7 +464,8 @@ pg_hba_file_rules(PG_FUNCTION_ARGS)
448464
*/
449465
static void
450466
fill_ident_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
451-
int lineno, IdentLine *ident, const char *err_msg)
467+
int map_number, int lineno, IdentLine *ident,
468+
const char *err_msg)
452469
{
453470
Datum values[NUM_PG_IDENT_FILE_MAPPINGS_ATTS];
454471
bool nulls[NUM_PG_IDENT_FILE_MAPPINGS_ATTS];
@@ -461,6 +478,12 @@ fill_ident_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
461478
memset(nulls, 0, sizeof(nulls));
462479
index = 0;
463480

481+
/* map_number, nothing on error */
482+
if (err_msg)
483+
nulls[index++] = true;
484+
else
485+
values[index++] = Int32GetDatum(map_number);
486+
464487
/* line_number */
465488
values[index++] = Int32GetDatum(lineno);
466489

@@ -473,7 +496,7 @@ fill_ident_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
473496
else
474497
{
475498
/* no parsing result, so set relevant fields to nulls */
476-
memset(&nulls[1], true, (NUM_PG_IDENT_FILE_MAPPINGS_ATTS - 2) * sizeof(bool));
499+
memset(&nulls[2], true, (NUM_PG_IDENT_FILE_MAPPINGS_ATTS - 3) * sizeof(bool));
477500
}
478501

479502
/* error */
@@ -495,6 +518,7 @@ fill_ident_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
495518
FILE *file;
496519
List *ident_lines = NIL;
497520
ListCell *line;
521+
int map_number = 0;
498522
MemoryContext linecxt;
499523
MemoryContext identcxt;
500524
MemoryContext oldcxt;
@@ -529,7 +553,12 @@ fill_ident_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
529553
if (tok_line->err_msg == NULL)
530554
identline = parse_ident_line(tok_line, DEBUG3);
531555

532-
fill_ident_line(tuple_store, tupdesc, tok_line->line_num, identline,
556+
/* no error, set a new mapping number */
557+
if (tok_line->err_msg == NULL)
558+
map_number++;
559+
560+
fill_ident_line(tuple_store, tupdesc, map_number,
561+
tok_line->line_num, identline,
533562
tok_line->err_msg);
534563
}
535564

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202210141
60+
#define CATALOG_VERSION_NO 202210261
6161

6262
#endif

src/include/catalog/pg_proc.dat

+6-5
Original file line numberDiff line numberDiff line change
@@ -6135,15 +6135,16 @@
61356135
{ oid => '3401', descr => 'show pg_hba.conf rules',
61366136
proname => 'pg_hba_file_rules', prorows => '1000', proretset => 't',
61376137
provolatile => 'v', prorettype => 'record', proargtypes => '',
6138-
proallargtypes => '{int4,text,_text,_text,text,text,text,_text,text}',
6139-
proargmodes => '{o,o,o,o,o,o,o,o,o}',
6140-
proargnames => '{line_number,type,database,user_name,address,netmask,auth_method,options,error}',
6138+
proallargtypes => '{int4,int4,text,_text,_text,text,text,text,_text,text}',
6139+
proargmodes => '{o,o,o,o,o,o,o,o,o,o}',
6140+
proargnames => '{rule_number,line_number,type,database,user_name,address,netmask,auth_method,options,error}',
61416141
prosrc => 'pg_hba_file_rules' },
61426142
{ oid => '6250', descr => 'show pg_ident.conf mappings',
61436143
proname => 'pg_ident_file_mappings', prorows => '1000', proretset => 't',
61446144
provolatile => 'v', prorettype => 'record', proargtypes => '',
6145-
proallargtypes => '{int4,text,text,text,text}', proargmodes => '{o,o,o,o,o}',
6146-
proargnames => '{line_number,map_name,sys_name,pg_username,error}',
6145+
proallargtypes => '{int4,int4,text,text,text,text}',
6146+
proargmodes => '{o,o,o,o,o,o}',
6147+
proargnames => '{map_number,line_number,map_name,sys_name,pg_username,error}',
61476148
prosrc => 'pg_ident_file_mappings' },
61486149
{ oid => '1371', descr => 'view system lock information',
61496150
proname => 'pg_lock_status', prorows => '1000', proretset => 't',

src/test/regress/expected/rules.out

+6-4
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,8 @@ pg_group| SELECT pg_authid.rolname AS groname,
13371337
WHERE (pg_auth_members.roleid = pg_authid.oid)) AS grolist
13381338
FROM pg_authid
13391339
WHERE (NOT pg_authid.rolcanlogin);
1340-
pg_hba_file_rules| SELECT a.line_number,
1340+
pg_hba_file_rules| SELECT a.rule_number,
1341+
a.line_number,
13411342
a.type,
13421343
a.database,
13431344
a.user_name,
@@ -1346,13 +1347,14 @@ pg_hba_file_rules| SELECT a.line_number,
13461347
a.auth_method,
13471348
a.options,
13481349
a.error
1349-
FROM pg_hba_file_rules() a(line_number, type, database, user_name, address, netmask, auth_method, options, error);
1350-
pg_ident_file_mappings| SELECT a.line_number,
1350+
FROM pg_hba_file_rules() a(rule_number, line_number, type, database, user_name, address, netmask, auth_method, options, error);
1351+
pg_ident_file_mappings| SELECT a.map_number,
1352+
a.line_number,
13511353
a.map_name,
13521354
a.sys_name,
13531355
a.pg_username,
13541356
a.error
1355-
FROM pg_ident_file_mappings() a(line_number, map_name, sys_name, pg_username, error);
1357+
FROM pg_ident_file_mappings() a(map_number, line_number, map_name, sys_name, pg_username, error);
13561358
pg_indexes| SELECT n.nspname AS schemaname,
13571359
c.relname AS tablename,
13581360
i.relname AS indexname,

0 commit comments

Comments
 (0)