Repository navigation
Expand file tree
/
Copy pathpg_fsck.c
More file actions
190 lines (164 loc) · 6.59 KB
/
Copy pathpg_fsck.c
File metadata and controls
190 lines (164 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-------------------------------------------------------------------------
*
* pg_fsck.c
* some utility functions
*
* pg_fsck/pg_fsck.c
*
* Copyright (c) 2022-2025, Xnerv Wang <xnervwang@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the <organization> nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "common/relpath.h"
#include "utils/builtins.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "access/htup_details.h"
#include "catalog/pg_authid_d.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "postmaster/syslogger.h"
#include "utils/acl.h"
PG_MODULE_MAGIC;
/*
* Returns the the current database ID of this connectionl
*/
PG_FUNCTION_INFO_V1(pg_fsck_get_my_database_id);
/*
* Returns the the current database directory path. If it's
* non-default tablespace, the symbolic path "pg_tblspc/..."
* will be returned rather than the actual physical path.
*/
PG_FUNCTION_INFO_V1(pg_fsck_get_database_path);
Datum
pg_fsck_get_my_database_id(PG_FUNCTION_ARGS)
{
PG_RETURN_OID(MyDatabaseId);
}
Datum
pg_fsck_get_database_path(PG_FUNCTION_ARGS)
{
Oid dbOid = PG_GETARG_OID(0);
Oid spcOid = PG_GETARG_OID(1);
PG_RETURN_TEXT_P(cstring_to_text(GetDatabasePath(dbOid, spcOid)));
}
/*
* Returns the file mode, owner uid/gid, and (for symlinks) the link target of
* a path. Unlike pg_stat_file(), which deliberately drops st_mode/st_uid/st_gid,
* this exposes them so callers can audit filesystem permissions and ownership.
* lstat() is used so a symlink is reported as itself rather than its target.
*/
PG_FUNCTION_INFO_V1(pg_fsck_stat);
/*
* Enforce the same read-path privilege model as pg_stat_file(): members of
* pg_read_server_files may stat any path; everyone else is confined to the
* data directory or the log directory. Replicated from the static
* convert_and_check_filename() in src/backend/utils/adt/genfile.c (not
* linkable from an extension), keeping byte-identical semantics.
*/
static char *
fsck_convert_and_check_filename(text *arg)
{
char *filename = text_to_cstring(arg);
canonicalize_path(filename); /* prevent ".." escapes */
if (has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
return filename;
if (is_absolute_path(filename))
{
/* Absolute path is allowed only within DataDir or Log_directory. */
if (!path_is_prefix_of_path(DataDir, filename) &&
(!is_absolute_path(Log_directory) ||
!path_is_prefix_of_path(Log_directory, filename)))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("absolute path not allowed")));
}
else if (!path_is_relative_and_below_cwd(filename))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("path must be in or below the data directory")));
return filename;
}
Datum
pg_fsck_stat(PG_FUNCTION_ARGS)
{
text *filename_t = PG_GETARG_TEXT_PP(0);
char *filename;
struct stat st;
Datum values[5];
bool isnull[5];
TupleDesc tupdesc;
HeapTuple tuple;
filename = fsck_convert_and_check_filename(filename_t);
/* lstat, not stat: report the symlink itself, not what it points at. */
if (lstat(filename, &st) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not stat file \"%s\": %m", filename)));
tupdesc = CreateTemplateTupleDesc(5);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "mode", INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "uid", INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "gid", INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "is_symlink", BOOLOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "symlink_target", TEXTOID, -1, 0);
TupleDescFinalize(tupdesc);
tupdesc = BlessTupleDesc(tupdesc);
memset(isnull, false, sizeof(isnull));
/* Raw st_mode; callers format the permission bits (mode & 0777) in SQL. */
values[0] = Int32GetDatum((int32) st.st_mode);
values[1] = Int32GetDatum((int32) st.st_uid);
values[2] = Int32GetDatum((int32) st.st_gid);
if (S_ISLNK(st.st_mode))
{
char target[MAXPGPATH];
ssize_t rllen = readlink(filename, target, sizeof(target));
if (rllen < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not read symbolic link \"%s\": %m", filename)));
if (rllen >= (ssize_t) sizeof(target))
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("symbolic link \"%s\" target is too long", filename)));
target[rllen] = '\0'; /* readlink does not NUL-terminate */
values[3] = BoolGetDatum(true);
values[4] = CStringGetTextDatum(target);
}
else
{
values[3] = BoolGetDatum(false);
isnull[4] = true;
}
tuple = heap_form_tuple(tupdesc, values, isnull);
pfree(filename);
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}