Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*-------------------------------------------------------------------------
 *
 * be-secure-common.c
 *
 * common implementation-independent SSL support code
 *
 * While be-secure.c contains the interfaces that the rest of the
 * communications code calls, this file contains support routines that are
 * used by the library-specific implementations such as be-secure-openssl.c.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *    src/backend/libpq/be-secure-common.c
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include <sys/stat.h>
#include <unistd.h>
 
#include "common/percentrepl.h"
#include "common/string.h"
#include "libpq/libpq.h"
#include "storage/fd.h"
#include "utils/builtins.h"
#include "utils/guc.h"
 
static HostsLine *parse_hosts_line(TokenizedAuthLine *tok_line, int elevel);
 
/*
 * Run ssl_passphrase_command
 *
 * prompt will be substituted for %p.  is_server_start determines the loglevel
 * of error messages from executing the command, the loglevel for failures in
 * param substitution will be ERROR regardless of is_server_start.  The actual
 * command used depends on the configuration for the current host.
 *
 * The result will be put in buffer buf, which is of size size.  The return
 * value is the length of the actual result.
 */
int
run_ssl_passphrase_command(const char *cmd, const char *prompt,
                           bool is_server_start, char *buf, int size)
{
    int         loglevel = is_server_start ? ERROR : LOG;
    char       *command;
    FILE       *fh;
    int         pclose_rc;
    size_t      len = 0;
 
    Assert(prompt);
    Assert(size > 0);
    buf[0] = '\0';
 
    command = replace_percent_placeholders(cmd, "ssl_passphrase_command", "p", prompt);
 
    fh = OpenPipeStream(command, "r");
    if (fh == NULL)
    {
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("could not execute command \"%s\": %m",
                        command)));
        goto error;
    }
 
    if (!fgets(buf, size, fh))
    {
        if (ferror(fh))
        {
            explicit_bzero(buf, size);
            ereport(loglevel,
                    (errcode_for_file_access(),
                     errmsg("could not read from command \"%s\": %m",
                            command)));
            goto error;
        }
    }
 
    pclose_rc = ClosePipeStream(fh);
    if (pclose_rc == -1)
    {
        explicit_bzero(buf, size);
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("could not close pipe to external command: %m")));
        goto error;
    }
    else if (pclose_rc != 0)
    {
        char       *reason;
 
        explicit_bzero(buf, size);
        reason = wait_result_to_str(pclose_rc);
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("command \"%s\" failed",
                        command),
                 errdetail_internal("%s", reason)));
        pfree(reason);
        goto error;
    }
 
    /* strip trailing newline and carriage return */
    len = pg_strip_crlf(buf);
 
error:
    pfree(command);
    return len;
}
 
 
/*
 * Check permissions for SSL key files.
 */
bool
check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)
{
    int         loglevel = isServerStart ? FATAL : LOG;
    struct stat buf;
 
    if (stat(ssl_key_file, &buf) != 0)
    {
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("could not access private key file \"%s\": %m",
                        ssl_key_file)));
        return false;
    }
 
    /* Key file must be a regular file */
    if (!S_ISREG(buf.st_mode))
    {
        ereport(loglevel,
                (errcode(ERRCODE_CONFIG_FILE_ERROR),
                 errmsg("private key file \"%s\" is not a regular file",
                        ssl_key_file)));
        return false;
    }
 
    /*
     * Refuse to load key files owned by users other than us or root, and
     * require no public access to the key file.  If the file is owned by us,
     * require mode 0600 or less.  If owned by root, require 0640 or less to
     * allow read access through either our gid or a supplementary gid that
     * allows us to read system-wide certificates.
     *
     * Note that roughly similar checks are performed in
     * src/interfaces/libpq/fe-secure-openssl.c so any changes here may need
     * to be made there as well.  The environment is different though; this
     * code can assume that we're not running as root.
     *
     * Ideally we would do similar permissions checks on Windows, but it is
     * not clear how that would work since Unix-style permissions may not be
     * available.
     */
#if !defined(WIN32) && !defined(__CYGWIN__)
    if (buf.st_uid != geteuid() && buf.st_uid != 0)
    {
        ereport(loglevel,
                (errcode(ERRCODE_CONFIG_FILE_ERROR),
                 errmsg("private key file \"%s\" must be owned by the database user or root",
                        ssl_key_file)));
        return false;
    }
 
    if ((buf.st_uid == geteuid() && buf.st_mode & (S_IRWXG | S_IRWXO)) ||
        (buf.st_uid == 0 && buf.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)))
    {
        ereport(loglevel,
                (errcode(ERRCODE_CONFIG_FILE_ERROR),
                 errmsg("private key file \"%s\" has group or world access",
                        ssl_key_file),
                 errdetail("File must have permissions u=rw (0600) or less if owned by the database user, or permissions u=rw,g=r (0640) or less if owned by root.")));
        return false;
    }
#endif
 
    return true;
}
 
/*
 * parse_hosts_line
 *
 * Parses a loaded line from the pg_hosts.conf configuration and pulls out the
 * hostname, certificate, key and CA parts in order to build an SNI config in
 * the TLS backend. Validation of the parsed values is left for the TLS backend
 * to implement.
 */
static HostsLine *
parse_hosts_line(TokenizedAuthLine *tok_line, int elevel)
{
    HostsLine  *parsedline;
    List       *tokens;
    ListCell   *field;
    AuthToken  *token;
 
    parsedline = palloc0(sizeof(HostsLine));
    parsedline->sourcefile = pstrdup(tok_line->file_name);
    parsedline->linenumber = tok_line->line_num;
    parsedline->rawline = pstrdup(tok_line->raw_line);
    parsedline->hostnames = NIL;
 
    /* Initialize optional fields */
    parsedline->ssl_passphrase_cmd = NULL;
    parsedline->ssl_passphrase_reload = false;
 
    /* Hostname */
    field = list_head(tok_line->fields);
    tokens = lfirst(field);
    foreach_ptr(AuthToken, hostname, tokens)
    {
        if ((tokens->length > 1) &&
            (strcmp(hostname->string, "*") == 0 || strcmp(hostname->string, "/no_sni/") == 0))
        {
            ereport(elevel,
                    errcode(ERRCODE_CONFIG_FILE_ERROR),
                    errmsg("default and non-SNI entries cannot be mixed with other entries"),
                    errcontext("line %d of configuration file \"%s\"",
                               tok_line->line_num, tok_line->file_name));
            return NULL;
        }
 
        parsedline->hostnames = lappend(parsedline->hostnames, pstrdup(hostname->string));
    }
 
    /* SSL Certificate (Required) */
    field = lnext(tok_line->fields, field);
    if (!field)
    {
        ereport(elevel,
                errcode(ERRCODE_CONFIG_FILE_ERROR),
                errmsg("missing entry at end of line"),
                errcontext("line %d of configuration file \"%s\"",
                           tok_line->line_num, tok_line->file_name));
        return NULL;
    }
    tokens = lfirst(field);
    if (tokens->length > 1)
    {
        ereport(elevel,
                errcode(ERRCODE_CONFIG_FILE_ERROR),
                errmsg("multiple values specified for SSL certificate"),
                errcontext("line %d of configuration file \"%s\"",
                           tok_line->line_num, tok_line->file_name));
        return NULL;
    }
    token = linitial(tokens);
    parsedline->ssl_cert = pstrdup(token->string);
 
    /* SSL key (Required) */
    field = lnext(tok_line->fields, field);
    if (!field)
    {
        ereport(elevel,
                errcode(ERRCODE_CONFIG_FILE_ERROR),
                errmsg("missing entry at end of line"),
                errcontext("line %d of configuration file \"%s\"",
                           tok_line->line_num, tok_line->file_name));
        return NULL;
    }
    tokens = lfirst(field);
    if (tokens->length > 1)
    {
        ereport(elevel,
                errcode(ERRCODE_CONFIG_FILE_ERROR),
                errmsg("multiple values specified for SSL key"),
                errcontext("line %d of configuration file \"%s\"",
                           tok_line->line_num, tok_line->file_name));
        return NULL;
    }
    token = linitial(tokens);
    parsedline->ssl_key = pstrdup(token->string);
 
    /* SSL CA (optional) */
    field = lnext(tok_line->fields, field);
    if (!field)
        return parsedline;
    tokens = lfirst(field);
    if (tokens->length > 1)
    {
        ereport(elevel,
                errcode(ERRCODE_CONFIG_FILE_ERROR),
                errmsg("multiple values specified for SSL CA"),
                errcontext("line %d of configuration file \"%s\"",
                           tok_line->line_num, tok_line->file_name));
        return NULL;
    }
    token = linitial(tokens);
    parsedline->ssl_ca = pstrdup(token->string);
 
    /* SSL Passphrase Command (optional) */
    field = lnext(tok_line->fields, field);
    if (field)
    {
        tokens = lfirst(field);
        if (tokens->length > 1)
        {
            ereport(elevel,
                    errcode(ERRCODE_CONFIG_FILE_ERROR),
                    errmsg("multiple values specified for SSL passphrase command"),
                    errcontext("line %d of configuration file \"%s\"",
                               tok_line->line_num, tok_line->file_name));
            return NULL;
        }
        token = linitial(tokens);
        parsedline->ssl_passphrase_cmd = pstrdup(token->string);
 
        /*
         * SSL Passphrase Command support reload (optional). This field is
         * only supported if there was a passphrase command parsed first, so
         * nest it under the previous token.
         */
        field = lnext(tok_line->fields, field);
        if (field)
        {
            tokens = lfirst(field);
            token = linitial(tokens);
 
            /*
             * There should be no more tokens after this, if there are break
             * parsing and report error to avoid silently accepting incorrect
             * config.
             */
            if (lnext(tok_line->fields, field))
            {
                ereport(elevel,
                        errcode(ERRCODE_CONFIG_FILE_ERROR),
                        errmsg("extra fields at end of line"),
                        errcontext("line %d of configuration file \"%s\"",
                                   tok_line->line_num, tok_line->file_name));
                return NULL;
            }
 
            if (tokens->length > 1 || !parse_bool(token->string, &parsedline->ssl_passphrase_reload))
            {
                ereport(elevel,
                        errcode(ERRCODE_CONFIG_FILE_ERROR),
                        errmsg("incorrect syntax for boolean value SSL_passphrase_cmd_reload"),
                        errcontext("line %d of configuration file \"%s\"",
                                   tok_line->line_num, tok_line->file_name));
                return NULL;
            }
        }
    }
 
    return parsedline;
}
 
/*
 * load_hosts
 *
 * Reads and parses the pg_hosts.conf configuration file and passes back a List
 * of HostsLine elements containing the parsed lines, or NIL in case of an empty
 * file.  The list is returned in the hosts parameter. The function will return
 * a HostsFileLoadResult value detailing the result of the operation.  When
 * the hosts configuration failed to load, the err_msg variable may have more
 * information in case it was passed as non-NULL.
 */
HostsFileLoadResult
load_hosts(List **hosts, char **err_msg)
{
    FILE       *file;
    ListCell   *line;
    List       *hosts_lines = NIL;
    List       *parsed_lines = NIL;
    HostsLine  *newline;
    bool        ok = true;
 
    /*
     * If we cannot return results then error out immediately. This implies
     * API misuse or a similar kind of programmer error.
     */
    if (!hosts)
    {
        if (err_msg)
            *err_msg = psprintf("cannot load config from \"%s\", return variable missing",
                                HostsFileName);
        return HOSTSFILE_LOAD_FAILED;
    }
    *hosts = NIL;
 
    /*
     * This is not an auth file per se, but it is using the same file format
     * as the pg_hba and pg_ident files and thus the same code infrastructure.
     * A future TODO might be to rename the supporting code with a more
     * generic name?
     */
    file = open_auth_file(HostsFileName, LOG, 0, err_msg);
    if (file == NULL)
    {
        if (errno == ENOENT)
            return HOSTSFILE_MISSING;
 
        return HOSTSFILE_LOAD_FAILED;
    }
 
    tokenize_auth_file(HostsFileName, file, &hosts_lines, LOG, 0);
 
    foreach(line, hosts_lines)
    {
        TokenizedAuthLine *tok_line = (TokenizedAuthLine *) lfirst(line);
 
        /*
         * Mark processing as not-ok in case lines are found with errors in
         * tokenization (.err_msg is set) or during parsing.
         */
        if ((tok_line->err_msg != NULL) ||
            ((newline = parse_hosts_line(tok_line, LOG)) == NULL))
        {
            ok = false;
            continue;
        }
 
        parsed_lines = lappend(parsed_lines, newline);
    }
 
    /* Free memory from tokenizer */
    free_auth_file(file, 0);
    *hosts = parsed_lines;
 
    if (!ok)
    {
        if (err_msg)
            *err_msg = psprintf("loading config from \"%s\" failed due to parsing error",
                                HostsFileName);
        return HOSTSFILE_LOAD_FAILED;
    }
 
    if (parsed_lines == NIL)
        return HOSTSFILE_EMPTY;
 
    return HOSTSFILE_LOAD_OK;
}