LCOV - code coverage report
Current view: top level - src/test/modules/ldap_password_func - ldap_password_func.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 15 17 88.2 %
Date: 2024-04-24 01:11:45 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * Copyright (c) 2022-2024, PostgreSQL Global Development Group
       4             :  *
       5             :  * ldap_password_func.c
       6             :  *
       7             :  * Loadable PostgreSQL module to mutate the ldapbindpasswd. This
       8             :  * implementation just hands back the configured password rot13'd.
       9             :  *
      10             :  *-------------------------------------------------------------------------
      11             :  */
      12             : 
      13             : #include "postgres.h"
      14             : 
      15             : #include <float.h>
      16             : #include <stdio.h>
      17             : 
      18             : #include "libpq/auth.h"
      19             : #include "libpq/libpq.h"
      20             : #include "libpq/libpq-be.h"
      21             : #include "utils/guc.h"
      22             : 
      23           8 : PG_MODULE_MAGIC;
      24             : 
      25             : void        _PG_init(void);
      26             : void        _PG_fini(void);
      27             : 
      28             : /* hook function */
      29             : static char *rot13_passphrase(char *password);
      30             : 
      31             : /*
      32             :  * Module load callback
      33             :  */
      34             : void
      35           8 : _PG_init(void)
      36             : {
      37           8 :     ldap_password_hook = rot13_passphrase;
      38           8 : }
      39             : 
      40             : void
      41           0 : _PG_fini(void)
      42             : {
      43             :     /* do  nothing yet */
      44           0 : }
      45             : 
      46             : static char *
      47           4 : rot13_passphrase(char *pw)
      48             : {
      49           4 :     size_t      size = strlen(pw) + 1;
      50             : 
      51           4 :     char       *new_pw = (char *) palloc(size);
      52             : 
      53           4 :     strlcpy(new_pw, pw, size);
      54          28 :     for (char *p = new_pw; *p; p++)
      55             :     {
      56          24 :         char        c = *p;
      57             : 
      58          24 :         if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
      59           8 :             *p = c + 13;
      60          16 :         else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
      61          14 :             *p = c - 13;
      62             :     }
      63             : 
      64           4 :     return new_pw;
      65             : }

Generated by: LCOV version 1.14