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 :
21 0 : 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
33 : */
34 : void
35 0 : _PG_init(void)
36 : {
37 : /* Define custom GUC variable. */
38 0 : 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,
47 : NULL);
48 :
49 0 : MarkGUCPrefixReserved("ssl_passphrase");
50 :
51 0 : if (ssl_passphrase)
52 0 : openssl_tls_init_hook = set_rot13;
53 0 : }
54 :
55 : static void
56 0 : set_rot13(SSL_CTX *context, bool isServerStart)
57 : {
58 : /* warn if the user has set ssl_passphrase_command */
59 0 : if (ssl_passphrase_command[0])
60 0 : ereport(WARNING,
61 : (errmsg("\"ssl_passphrase_command\" setting ignored by ssl_passphrase_func module")));
62 :
63 0 : SSL_CTX_set_default_passwd_cb(context, rot13_passphrase);
64 0 : }
65 :
66 : static int
67 0 : rot13_passphrase(char *buf, int size, int rwflag, void *userdata)
68 : {
69 :
70 0 : Assert(ssl_passphrase != NULL);
71 0 : strlcpy(buf, ssl_passphrase, size);
72 0 : for (char *p = buf; *p; p++)
73 : {
74 0 : char c = *p;
75 :
76 0 : if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
77 0 : *p = c + 13;
78 0 : else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
79 0 : *p = c - 13;
80 0 : }
81 :
82 0 : return strlen(buf);
83 : }
|