LCOV - differential code coverage report
Current view: top level - src/test/modules/ssl_passphrase_callback - ssl_passphrase_func.c (source / functions) Coverage Total Hit GIC CBC ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 100.0 % 22 22 14 8 14
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 4 4 3 1 3
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a