LCOV - code coverage report
Current view: top level - src/test/modules/ssl_passphrase_callback - ssl_passphrase_func.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 21 21 100.0 %
Date: 2024-11-21 09:14:53 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14