aboutsummaryrefslogtreecommitdiff
path: root/src/test/modules/ldap_password_func/ldap_password_func.c
blob: 79c023bfc68c985ea92bfde057882a9af632f5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*-------------------------------------------------------------------------
 *
 * Copyright (c) 2022-2025, PostgreSQL Global Development Group
 *
 * ldap_password_func.c
 *
 * Loadable PostgreSQL module to mutate the ldapbindpasswd. This
 * implementation just hands back the configured password rot13'd.
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include <float.h>
#include <stdio.h>

#include "fmgr.h"
#include "libpq/auth.h"

PG_MODULE_MAGIC;

void		_PG_init(void);

/* hook function */
static char *rot13_passphrase(char *password);

/*
 * Module load callback
 */
void
_PG_init(void)
{
	ldap_password_hook = rot13_passphrase;
}

static char *
rot13_passphrase(char *pw)
{
	size_t		size = strlen(pw) + 1;

	char	   *new_pw = (char *) palloc(size);

	strlcpy(new_pw, pw, size);
	for (char *p = new_pw; *p; p++)
	{
		char		c = *p;

		if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
			*p = c + 13;
		else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
			*p = c - 13;
	}

	return new_pw;
}