Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * ssl_passphrase_func.c
4 : *
5 : * Loadable PostgreSQL module fetch an ssl passphrase for the server cert.
6 : * instead of calling an external program. This implementation just hands
7 : * back the configured password rot13'd.
8 : *
9 : *-------------------------------------------------------------------------
10 : */
11 :
12 : #include "postgres.h"
13 :
14 : #include <float.h>
15 : #include <stdio.h>
16 :
17 : #include "libpq/libpq.h"
18 : #include "libpq/libpq-be.h"
19 : #include "utils/guc.h"
20 :
1110 andrew 21 CBC 3 : PG_MODULE_MAGIC;
22 :
23 : static char *ssl_passphrase = NULL;
24 :
25 : /* callback function */
26 : static int rot13_passphrase(char *buf, int size, int rwflag, void *userdata);
27 :
28 : /* hook function to set the callback */
29 : static void set_rot13(SSL_CTX *context, bool isServerStart);
30 :
31 : /*
32 : * Module load callback
1110 andrew 33 ECB : */
34 : void
1110 andrew 35 GIC 3 : _PG_init(void)
1110 andrew 36 ECB : {
37 : /* Define custom GUC variable. */
1110 andrew 38 GIC 3 : DefineCustomStringVariable("ssl_passphrase.passphrase",
39 : "passphrase before transformation",
40 : NULL,
41 : &ssl_passphrase,
42 : NULL,
43 : PGC_SIGHUP,
44 : 0, /* no flags required */
45 : NULL,
46 : NULL,
1110 andrew 47 ECB : NULL);
48 :
412 tgl 49 CBC 3 : MarkGUCPrefixReserved("ssl_passphrase");
474 tgl 50 ECB :
1110 andrew 51 CBC 3 : if (ssl_passphrase)
1110 andrew 52 GIC 3 : openssl_tls_init_hook = set_rot13;
53 3 : }
1110 andrew 54 ECB :
55 : static void
1110 andrew 56 GIC 3 : set_rot13(SSL_CTX *context, bool isServerStart)
1110 andrew 57 ECB : {
58 : /* warn if the user has set ssl_passphrase_command */
1060 tgl 59 GIC 3 : if (ssl_passphrase_command[0])
1110 andrew 60 2 : ereport(WARNING,
1110 andrew 61 ECB : (errmsg("ssl_passphrase_command setting ignored by ssl_passphrase_func module")));
62 :
1110 andrew 63 GIC 3 : SSL_CTX_set_default_passwd_cb(context, rot13_passphrase);
64 3 : }
1110 andrew 65 ECB :
66 : static int
1110 andrew 67 GIC 3 : rot13_passphrase(char *buf, int size, int rwflag, void *userdata)
1110 andrew 68 ECB : {
69 :
1110 andrew 70 CBC 3 : Assert(ssl_passphrase != NULL);
972 peter 71 GIC 3 : strlcpy(buf, ssl_passphrase, size);
1110 andrew 72 CBC 23 : for (char *p = buf; *p; p++)
73 : {
74 20 : char c = *p;
1110 andrew 75 ECB :
1110 andrew 76 CBC 20 : if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
77 10 : *p = c + 13;
1110 andrew 78 GIC 10 : else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
79 8 : *p = c - 13;
1110 andrew 80 ECB : }
81 :
1110 andrew 82 GIC 3 : return strlen(buf);
83 : }
|