myldapsync 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of myldapsync might be problematic. Click here for more details.
- myldapsync-1.0.2/LICENSE +23 -0
- myldapsync-1.0.2/MANIFEST.in +3 -0
- myldapsync-1.0.2/PKG-INFO +78 -0
- myldapsync-1.0.2/README.md +55 -0
- myldapsync-1.0.2/myldapsync/__init__.py +294 -0
- myldapsync-1.0.2/myldapsync/config.ini.example +144 -0
- myldapsync-1.0.2/myldapsync/config_default.ini +62 -0
- myldapsync-1.0.2/myldapsync/ldaputils/__init__.py +7 -0
- myldapsync-1.0.2/myldapsync/ldaputils/connection.py +127 -0
- myldapsync-1.0.2/myldapsync/ldaputils/users.py +80 -0
- myldapsync-1.0.2/myldapsync/myutils/__init__.py +7 -0
- myldapsync-1.0.2/myldapsync/myutils/users.py +216 -0
- myldapsync-1.0.2/myldapsync.egg-info/PKG-INFO +78 -0
- myldapsync-1.0.2/myldapsync.egg-info/SOURCES.txt +19 -0
- myldapsync-1.0.2/myldapsync.egg-info/dependency_links.txt +1 -0
- myldapsync-1.0.2/myldapsync.egg-info/entry_points.txt +2 -0
- myldapsync-1.0.2/myldapsync.egg-info/requires.txt +3 -0
- myldapsync-1.0.2/myldapsync.egg-info/top_level.txt +1 -0
- myldapsync-1.0.2/requirements.txt +3 -0
- myldapsync-1.0.2/setup.cfg +4 -0
- myldapsync-1.0.2/setup.py +47 -0
myldapsync-1.0.2/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024, Artur Lebedev
|
|
6
|
+
|
|
7
|
+
Copyright (C) 2018 - 2023, EnterpriseDB Corporation
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and distribute this software and its
|
|
10
|
+
documentation for any purpose, without fee, and without a written agreement is
|
|
11
|
+
hereby granted, provided that the above copyright notice and this paragraph and
|
|
12
|
+
the following two paragraphs appear in all copies.
|
|
13
|
+
|
|
14
|
+
IN NO EVENT SHALL ENTERPRISEDB CORPORATION BE LIABLE TO ANY PARTY FOR DIRECT,
|
|
15
|
+
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
|
|
16
|
+
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
|
|
17
|
+
ENTERPRISEDB CORPORATION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
18
|
+
|
|
19
|
+
ENTERPRISEDB CORPORATION SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
|
|
20
|
+
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
|
|
22
|
+
ENTERPRISEDB CORPORATION HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
|
|
23
|
+
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: myldapsync
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: Synchronise LDAP users to MySQL
|
|
5
|
+
Home-page: https://github.com/6eh01der/myldapsync
|
|
6
|
+
Author: Artur Lebedev
|
|
7
|
+
Author-email: ras_atari@mail.ru
|
|
8
|
+
License: MySQL
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Requires-Python: >=3.7
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: ldap3>=2.9.1
|
|
21
|
+
Requires-Dist: mysql-connector-python>=8.0.31
|
|
22
|
+
Requires-Dist: pyasn1>=0.4.8
|
|
23
|
+
|
|
24
|
+
# myldapsync - fork of [pgldapsync](https://github.com/EnterpriseDB/pgldapsync) by [EnterpriseDB Corporation](https://www.enterprisedb.com/) adopted for MySQL and MariaDB
|
|
25
|
+
|
|
26
|
+
This Python module allows you to synchronise MySQL or MariaDB users
|
|
27
|
+
with users in an LDAP directory.
|
|
28
|
+
|
|
29
|
+
Require MySQL 8 with installed and configured simple or sasl LDAP Authentication Plugin or PAM plugin (pam also supported in MySQL 5.7 and MariaDB).
|
|
30
|
+
|
|
31
|
+
Percona Server for MySQL:
|
|
32
|
+
|
|
33
|
+
https://docs.percona.com/percona-server/8.0/ldap-authentication
|
|
34
|
+
|
|
35
|
+
https://docs.percona.com/percona-server/8.0/pam-plugin.html
|
|
36
|
+
|
|
37
|
+
MySQL:
|
|
38
|
+
|
|
39
|
+
https://dev.mysql.com/doc/refman/8.0/en/ldap-pluggable-authentication.html
|
|
40
|
+
|
|
41
|
+
https://dev.mysql.com/doc/refman/8.0/en/pam-pluggable-authentication.html
|
|
42
|
+
|
|
43
|
+
MariaDB:
|
|
44
|
+
|
|
45
|
+
https://mariadb.com/kb/en/authentication-plugin-pam/
|
|
46
|
+
|
|
47
|
+
*myldapsync is supported on Python 3.7 or later.*
|
|
48
|
+
|
|
49
|
+
In order to use it, you will need to create a _config.ini_
|
|
50
|
+
file containing the site-specific configuration you require.
|
|
51
|
+
See _config.ini.example_ for a complete list of all the
|
|
52
|
+
available configuration options. This file should be copied to
|
|
53
|
+
create your own configuration.
|
|
54
|
+
|
|
55
|
+
Once configured, simply run myldapsync like so:
|
|
56
|
+
|
|
57
|
+
python3 myldapsync.py /path/to/config.ini
|
|
58
|
+
|
|
59
|
+
In order to test the configuration (and dump the SQL that would
|
|
60
|
+
be executed to stdout), run it like this:
|
|
61
|
+
|
|
62
|
+
python3 myldapsync.py --dry-run /path/to/config.ini
|
|
63
|
+
|
|
64
|
+
## Creating a virtual environment for dev/test
|
|
65
|
+
|
|
66
|
+
python3 -m venv /path/to/myldapsync
|
|
67
|
+
source /path/to/myldapsync/bin/activate
|
|
68
|
+
pip install -r requirements.txt
|
|
69
|
+
|
|
70
|
+
Adapt the first command as required for your environment/Python
|
|
71
|
+
version.
|
|
72
|
+
|
|
73
|
+
## Creating a package
|
|
74
|
+
|
|
75
|
+
To create a package (wheel), run the following in your virtual
|
|
76
|
+
environment:
|
|
77
|
+
|
|
78
|
+
python3 setup.py sdist bdist_wheel --universal
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# myldapsync - fork of [pgldapsync](https://github.com/EnterpriseDB/pgldapsync) by [EnterpriseDB Corporation](https://www.enterprisedb.com/) adopted for MySQL and MariaDB
|
|
2
|
+
|
|
3
|
+
This Python module allows you to synchronise MySQL or MariaDB users
|
|
4
|
+
with users in an LDAP directory.
|
|
5
|
+
|
|
6
|
+
Require MySQL 8 with installed and configured simple or sasl LDAP Authentication Plugin or PAM plugin (pam also supported in MySQL 5.7 and MariaDB).
|
|
7
|
+
|
|
8
|
+
Percona Server for MySQL:
|
|
9
|
+
|
|
10
|
+
https://docs.percona.com/percona-server/8.0/ldap-authentication
|
|
11
|
+
|
|
12
|
+
https://docs.percona.com/percona-server/8.0/pam-plugin.html
|
|
13
|
+
|
|
14
|
+
MySQL:
|
|
15
|
+
|
|
16
|
+
https://dev.mysql.com/doc/refman/8.0/en/ldap-pluggable-authentication.html
|
|
17
|
+
|
|
18
|
+
https://dev.mysql.com/doc/refman/8.0/en/pam-pluggable-authentication.html
|
|
19
|
+
|
|
20
|
+
MariaDB:
|
|
21
|
+
|
|
22
|
+
https://mariadb.com/kb/en/authentication-plugin-pam/
|
|
23
|
+
|
|
24
|
+
*myldapsync is supported on Python 3.7 or later.*
|
|
25
|
+
|
|
26
|
+
In order to use it, you will need to create a _config.ini_
|
|
27
|
+
file containing the site-specific configuration you require.
|
|
28
|
+
See _config.ini.example_ for a complete list of all the
|
|
29
|
+
available configuration options. This file should be copied to
|
|
30
|
+
create your own configuration.
|
|
31
|
+
|
|
32
|
+
Once configured, simply run myldapsync like so:
|
|
33
|
+
|
|
34
|
+
python3 myldapsync.py /path/to/config.ini
|
|
35
|
+
|
|
36
|
+
In order to test the configuration (and dump the SQL that would
|
|
37
|
+
be executed to stdout), run it like this:
|
|
38
|
+
|
|
39
|
+
python3 myldapsync.py --dry-run /path/to/config.ini
|
|
40
|
+
|
|
41
|
+
## Creating a virtual environment for dev/test
|
|
42
|
+
|
|
43
|
+
python3 -m venv /path/to/myldapsync
|
|
44
|
+
source /path/to/myldapsync/bin/activate
|
|
45
|
+
pip install -r requirements.txt
|
|
46
|
+
|
|
47
|
+
Adapt the first command as required for your environment/Python
|
|
48
|
+
version.
|
|
49
|
+
|
|
50
|
+
## Creating a package
|
|
51
|
+
|
|
52
|
+
To create a package (wheel), run the following in your virtual
|
|
53
|
+
environment:
|
|
54
|
+
|
|
55
|
+
python3 setup.py sdist bdist_wheel --universal
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
8
|
+
|
|
9
|
+
"""myldapsync main entry point."""
|
|
10
|
+
|
|
11
|
+
# FIX THIS!
|
|
12
|
+
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
import os
|
|
16
|
+
|
|
17
|
+
import configparser
|
|
18
|
+
|
|
19
|
+
from myldapsync.ldaputils.connection import connect_ldap_server
|
|
20
|
+
from myldapsync.ldaputils.users import *
|
|
21
|
+
from myldapsync.myutils.users import *
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def read_command_line():
|
|
25
|
+
"""Read the command line arguments.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
ArgumentParser: The parsed arguments object
|
|
29
|
+
"""
|
|
30
|
+
parser = argparse.ArgumentParser(
|
|
31
|
+
description='Synchronise users and groups from LDAP/AD to MySQL.')
|
|
32
|
+
parser.add_argument("--dry-run", "-d", action='store_true',
|
|
33
|
+
help="don't apply changes to the database server, "
|
|
34
|
+
"dump the SQL to stdout instead")
|
|
35
|
+
parser.add_argument("config", metavar="CONFIG_FILE",
|
|
36
|
+
help="the configuration file to read")
|
|
37
|
+
|
|
38
|
+
args = parser.parse_args()
|
|
39
|
+
|
|
40
|
+
return args
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def read_config(file):
|
|
44
|
+
"""Read the config file.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
file (str): The config file to read
|
|
48
|
+
my_user (str[]): A list of users in MySQL
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
ConfigParser: The config object
|
|
52
|
+
"""
|
|
53
|
+
config = configparser.ConfigParser()
|
|
54
|
+
|
|
55
|
+
# Read the default config
|
|
56
|
+
defaults = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
|
57
|
+
'config_default.ini')
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
config.read(defaults)
|
|
61
|
+
except configparser.Error as exception:
|
|
62
|
+
sys.stderr.write(
|
|
63
|
+
"Error reading default configuration file (%s): %s\n" %
|
|
64
|
+
(defaults, exception))
|
|
65
|
+
sys.exit(1)
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
config.read(file)
|
|
69
|
+
except configparser.Error as exception:
|
|
70
|
+
sys.stderr.write(
|
|
71
|
+
"Error reading user configuration file (%s): %s\n" %
|
|
72
|
+
(file, exception))
|
|
73
|
+
sys.exit(1)
|
|
74
|
+
|
|
75
|
+
return config
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def main():
|
|
79
|
+
"""The core structure of the app."""
|
|
80
|
+
|
|
81
|
+
# Read the command line options
|
|
82
|
+
args = read_command_line()
|
|
83
|
+
|
|
84
|
+
# Read the config file
|
|
85
|
+
config = read_config(args.config)
|
|
86
|
+
|
|
87
|
+
# MySQL connection dictionary
|
|
88
|
+
connection_string_params = {}
|
|
89
|
+
|
|
90
|
+
if config.get('mysql', 'host') != '':
|
|
91
|
+
connection_string_params = dict(connection_string_params, host=config.get('mysql', 'host'))
|
|
92
|
+
|
|
93
|
+
if config.get('mysql', 'user') != '':
|
|
94
|
+
connection_string_params = dict(connection_string_params, user=config.get('mysql', 'user'))
|
|
95
|
+
|
|
96
|
+
if config.get('mysql', 'password') != '':
|
|
97
|
+
connection_string_params = dict(connection_string_params, password=config.get('mysql', 'password'))
|
|
98
|
+
|
|
99
|
+
if config.get('mysql', 'db') != '':
|
|
100
|
+
connection_string_params = dict(connection_string_params, db=config.get('mysql', 'db'))
|
|
101
|
+
|
|
102
|
+
if config.get('mysql', 'auth_plugin') != '':
|
|
103
|
+
connection_string_params = dict(connection_string_params, auth_plugin=config.get('mysql', 'auth_plugin'))
|
|
104
|
+
|
|
105
|
+
if config.get('mysql', 'ssl_ca') != '':
|
|
106
|
+
connection_string_params = dict(connection_string_params, ssl_ca=config.get('mysql', 'ssl_ca'))
|
|
107
|
+
|
|
108
|
+
if config.get('mysql', 'ssl_cert') != '':
|
|
109
|
+
connection_string_params = dict(connection_string_params, ssl_cert=config.get('mysql', 'ssl_cert'))
|
|
110
|
+
|
|
111
|
+
if config.get('mysql', 'ssl_key') != '':
|
|
112
|
+
connection_string_params = dict(connection_string_params, ssl_key=config.get('mysql', 'ssl_key'))
|
|
113
|
+
|
|
114
|
+
if config.get('mysql', 'ssl_verify_identity') != '':
|
|
115
|
+
connection_string_params = dict(connection_string_params, ssl_verify_identity=config.get('mysql', 'ssl_verify_identity'))
|
|
116
|
+
|
|
117
|
+
if config.get('mysql', 'ssl_verify_cert') != '':
|
|
118
|
+
connection_string_params = dict(connection_string_params, ssl_verify_cert=config.get('mysql', 'ssl_verify_cert'))
|
|
119
|
+
|
|
120
|
+
if config.get('mysql', 'use_pure') != '':
|
|
121
|
+
connection_string_params = dict(connection_string_params, use_pure=config.get('mysql', 'use_pure'))
|
|
122
|
+
|
|
123
|
+
if config.get('mysql', 'SERVICE_NAME') and config.get('mysql', 'LDAP_SERVER_IP') != '':
|
|
124
|
+
SERVICE_NAME = config.get('mysql', 'SERVICE_NAME')
|
|
125
|
+
LDAP_SERVER_IP = config.get('mysql', 'LDAP_SERVER_IP')
|
|
126
|
+
connection_string_params = dict(connection_string_params, krb_service_principal=f"{SERVICE_NAME}/{LDAP_SERVER_IP}")
|
|
127
|
+
|
|
128
|
+
# Connect to LDAP and get the users we care about
|
|
129
|
+
ldap_conn = connect_ldap_server(config)
|
|
130
|
+
if ldap_conn is None:
|
|
131
|
+
sys.exit(1)
|
|
132
|
+
|
|
133
|
+
ldap_users = get_filtered_ldap_users(config, ldap_conn, False)
|
|
134
|
+
if ldap_users is None:
|
|
135
|
+
sys.exit(1)
|
|
136
|
+
|
|
137
|
+
# Get the LDAP admin users, if the base DN and filter are configured
|
|
138
|
+
if config.get('ldap', 'admin_base_dn') == '' or \
|
|
139
|
+
config.get('ldap', 'admin_filter_string') == '':
|
|
140
|
+
ldap_admin_users = []
|
|
141
|
+
else:
|
|
142
|
+
ldap_admin_users = get_ldap_users(config, ldap_conn, True)
|
|
143
|
+
if ldap_admin_users is None:
|
|
144
|
+
sys.exit(1)
|
|
145
|
+
|
|
146
|
+
# Connect to MySQL and get the users we care about
|
|
147
|
+
my_conn = mysql.connector.connect(**connection_string_params)
|
|
148
|
+
if my_conn is None:
|
|
149
|
+
sys.exit(1)
|
|
150
|
+
|
|
151
|
+
my_users = get_filtered_my_users(config, my_conn)
|
|
152
|
+
if my_users is None:
|
|
153
|
+
sys.exit(1)
|
|
154
|
+
|
|
155
|
+
# Compare the LDAP and MySQL users and get the lists of users
|
|
156
|
+
# to add and drop.
|
|
157
|
+
users_to_create = get_create_users(ldap_users, my_users)
|
|
158
|
+
users_to_drop = get_drop_users(ldap_users, my_users)
|
|
159
|
+
|
|
160
|
+
# Create/drop users if required
|
|
161
|
+
have_work = ((config.getboolean('general',
|
|
162
|
+
'add_ldap_users_to_mysql') and
|
|
163
|
+
len(users_to_create) > 0) or
|
|
164
|
+
(config.getboolean('general',
|
|
165
|
+
'remove_users_from_mysql') and
|
|
166
|
+
len(users_to_drop) > 0))
|
|
167
|
+
|
|
168
|
+
# Initialise the counters for operations/errors
|
|
169
|
+
users_added = 0
|
|
170
|
+
users_dropped = 0
|
|
171
|
+
users_add_errors = 0
|
|
172
|
+
users_drop_errors = 0
|
|
173
|
+
|
|
174
|
+
# Warn the user we're in dry run mode
|
|
175
|
+
if args.dry_run:
|
|
176
|
+
print("-- This is an LDAP sync dry run.")
|
|
177
|
+
print("-- The commands below can be manually executed if required.")
|
|
178
|
+
|
|
179
|
+
cur = None
|
|
180
|
+
if have_work:
|
|
181
|
+
|
|
182
|
+
# Begin the transaction
|
|
183
|
+
if args.dry_run:
|
|
184
|
+
print("START TRANSACTION;")
|
|
185
|
+
else:
|
|
186
|
+
cur = my_conn.cursor()
|
|
187
|
+
cur.execute("START TRANSACTION;")
|
|
188
|
+
|
|
189
|
+
# Set authentication plugin
|
|
190
|
+
auth_plugin = config.get('general', 'auth_plugin')
|
|
191
|
+
if auth_plugin == 'simple':
|
|
192
|
+
identified = 'WITH authentication_ldap_simple'
|
|
193
|
+
elif auth_plugin == 'sasl':
|
|
194
|
+
identified = 'WITH authentication_ldap_sasl'
|
|
195
|
+
elif auth_plugin == 'pam':
|
|
196
|
+
dbms = config.get('general', 'dbms')
|
|
197
|
+
if dbms == 'mysql':
|
|
198
|
+
identified = 'WITH authentication_pam'
|
|
199
|
+
elif dbms == 'psms':
|
|
200
|
+
compat = config.get('general', 'compat')
|
|
201
|
+
if compat:
|
|
202
|
+
identified = 'WITH auth_pam_compat'
|
|
203
|
+
else:
|
|
204
|
+
identified = 'WITH auth_pam'
|
|
205
|
+
elif dbms == 'mariadb':
|
|
206
|
+
identified = 'VIA pam'
|
|
207
|
+
|
|
208
|
+
# If we need to add users to MySQL, then do so
|
|
209
|
+
if config.getboolean('general', 'add_ldap_users_to_mysql'):
|
|
210
|
+
|
|
211
|
+
# For each user, get the required attributes and SQL snippets
|
|
212
|
+
for user in users_to_create:
|
|
213
|
+
user_name = user.replace('\'', '\\\'')
|
|
214
|
+
user_grants = get_user_grants(config, user_name)
|
|
215
|
+
user_admin_grants = get_user_grants(config, user_name,
|
|
216
|
+
(user in ldap_admin_users))
|
|
217
|
+
privilege_list = get_user_privileges(config, user_name,
|
|
218
|
+
(user in ldap_admin_users))
|
|
219
|
+
|
|
220
|
+
if args.dry_run:
|
|
221
|
+
|
|
222
|
+
# It's a dry run, so just print the output
|
|
223
|
+
print('CREATE USER "%s" IDENTIFIED %s; %s %s %s' %
|
|
224
|
+
(user_name, identified, privilege_list, user_grants, user_admin_grants))
|
|
225
|
+
else:
|
|
226
|
+
|
|
227
|
+
# This is a live run, so directly execute the SQL generated.
|
|
228
|
+
# For each statement, create a savepoint so we can rollback
|
|
229
|
+
# to it if there's an error. That allows us to fail only
|
|
230
|
+
# a single user rather than all of them.
|
|
231
|
+
try:
|
|
232
|
+
# We can't use a real parameterised query here as we're
|
|
233
|
+
# working with an object, not data.
|
|
234
|
+
cur.execute('CREATE USER "%s" IDENTIFIED %s;' %
|
|
235
|
+
(user_name, identified))
|
|
236
|
+
cur.execute('%s' % privilege_list)
|
|
237
|
+
cur.execute('%s' % user_grants)
|
|
238
|
+
cur.execute('%s' % user_admin_grants)
|
|
239
|
+
users_added = users_added + 1
|
|
240
|
+
except mysql.connector.Error as exception:
|
|
241
|
+
sys.stderr.write("Error creating user %s: %s" % (user,
|
|
242
|
+
exception))
|
|
243
|
+
users_add_errors = users_add_errors + 1
|
|
244
|
+
cur.execute('ROLLBACK;')
|
|
245
|
+
|
|
246
|
+
# If we need to drop users from MySQL, then do so
|
|
247
|
+
if config.getboolean('general', 'remove_users_from_mysql'):
|
|
248
|
+
|
|
249
|
+
# For each user to drop, just run the DROP statement
|
|
250
|
+
for user in users_to_drop:
|
|
251
|
+
|
|
252
|
+
user_name = user.replace('\'', '\\\'')
|
|
253
|
+
|
|
254
|
+
if args.dry_run:
|
|
255
|
+
|
|
256
|
+
# It's a dry run, so just print the output
|
|
257
|
+
print('DROP USER "%s";' % user_name)
|
|
258
|
+
else:
|
|
259
|
+
|
|
260
|
+
# This is a live run, so directly execute the SQL generated.
|
|
261
|
+
# For each statement, create a savepoint so we can rollback
|
|
262
|
+
# to it if there's an error. That allows us to fail only
|
|
263
|
+
# a single user rather than all of them.
|
|
264
|
+
try:
|
|
265
|
+
# We can't use a real parameterised query here as we're
|
|
266
|
+
# working with an object, not data.
|
|
267
|
+
cur.execute('DROP USER "%s";' % user_name)
|
|
268
|
+
users_dropped = users_dropped + 1
|
|
269
|
+
except mysql.connector.Error as exception:
|
|
270
|
+
sys.stderr.write("Error dropping user %s: %s" % (user,
|
|
271
|
+
exception))
|
|
272
|
+
users_drop_errors = users_drop_errors + 1
|
|
273
|
+
cur.execute('ROLLBACK;')
|
|
274
|
+
|
|
275
|
+
if have_work:
|
|
276
|
+
|
|
277
|
+
# Commit the transaction
|
|
278
|
+
if args.dry_run:
|
|
279
|
+
print("COMMIT;")
|
|
280
|
+
else:
|
|
281
|
+
cur.execute("COMMIT;")
|
|
282
|
+
cur.close()
|
|
283
|
+
|
|
284
|
+
# Print the summary of work completed
|
|
285
|
+
print("Users added to MySQL: %d" % users_added)
|
|
286
|
+
print("Users dropped from MySQL: %d" % users_dropped)
|
|
287
|
+
if users_add_errors > 0:
|
|
288
|
+
print("Errors adding users: %d" %
|
|
289
|
+
users_add_errors)
|
|
290
|
+
if users_drop_errors > 0:
|
|
291
|
+
print("Errors dropping users: %d" %
|
|
292
|
+
users_drop_errors)
|
|
293
|
+
else:
|
|
294
|
+
print("No users were added or dropped.")
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
##########################################################################
|
|
2
|
+
#
|
|
3
|
+
# config.ini.example
|
|
4
|
+
#
|
|
5
|
+
# This file is a template for your site-specific configuration. Copy it,
|
|
6
|
+
# and modify to suit your requirements.
|
|
7
|
+
#
|
|
8
|
+
# Some values have defaults which are specified in config_default.ini.
|
|
9
|
+
# Others *must* be set in your configuration file.
|
|
10
|
+
#
|
|
11
|
+
##########################################################################
|
|
12
|
+
|
|
13
|
+
##########################################################################
|
|
14
|
+
# LDAP access configuration
|
|
15
|
+
##########################################################################
|
|
16
|
+
|
|
17
|
+
[ldap]
|
|
18
|
+
|
|
19
|
+
# Debug LDAP connections?
|
|
20
|
+
debug = false
|
|
21
|
+
|
|
22
|
+
# LDAP server connection details (REQUIRED)
|
|
23
|
+
server_uri = ldap://ldap.example.com
|
|
24
|
+
|
|
25
|
+
# User to bind to the directory as. Leave empty for anonymous binding.
|
|
26
|
+
bind_username =
|
|
27
|
+
bind_password =
|
|
28
|
+
|
|
29
|
+
# Use TLS? If the URI scheme is ldaps://, this is ignored.
|
|
30
|
+
use_starttls = false
|
|
31
|
+
|
|
32
|
+
# TLS/SSL certificates. Specify if required, otherwise leave empty
|
|
33
|
+
ca_cert_file =
|
|
34
|
+
cert_file =
|
|
35
|
+
key_file =
|
|
36
|
+
|
|
37
|
+
# Search scope for users (one of BASE, LEVEL or SUBTREE)
|
|
38
|
+
search_scope = LEVEL
|
|
39
|
+
|
|
40
|
+
# The base DN for the user search (REQUIRED)
|
|
41
|
+
base_dn = CN=Users,dc=example,dc=com
|
|
42
|
+
|
|
43
|
+
# Filter string for the user search. For OpenLDAP, '(cn=*)' may well be enough.
|
|
44
|
+
# For AD, you might use '(objectClass=user)' (REQUIRED)
|
|
45
|
+
filter_string = (objectClass=user)
|
|
46
|
+
|
|
47
|
+
# The base DN for the admin user search. This may be the same as the base_dn, or
|
|
48
|
+
# may point to an alternate OU. If empty, no search will be performed.
|
|
49
|
+
admin_base_dn = CN=Users,dc=example,dc=com
|
|
50
|
+
|
|
51
|
+
# The filter string for users that should be created as superusers regardless of
|
|
52
|
+
# the [general]/role_attribute_superuser setting. With AD you might use a filter
|
|
53
|
+
# such as: (&(objectClass=user)(memberof=CN=MYGROUP,CN=Users,dc=example,dc=com))
|
|
54
|
+
# If, for example on OpenLDAP, your admin users are members of an alternate OU,
|
|
55
|
+
# then a filter of (cn=*) may suffice.
|
|
56
|
+
# If empty, no search will be performed.
|
|
57
|
+
admin_filter_string = (&(objectClass=user)(memberof=CN=MYGROUP,CN=Users,dc=example,dc=com))
|
|
58
|
+
|
|
59
|
+
# The LDAP attribute containing user names. In OpenLDAP, this may be 'uid'
|
|
60
|
+
# whilst in AD, 'sAMAccountName' might be appropriate. (REQUIRED)
|
|
61
|
+
username_attribute = uid
|
|
62
|
+
|
|
63
|
+
# A command delimited list of users to ignore
|
|
64
|
+
ignore_users = Manager,ldap.sync
|
|
65
|
+
|
|
66
|
+
# Kerberos authentication parameters
|
|
67
|
+
use_kerberos =
|
|
68
|
+
SERVICE_NAME =
|
|
69
|
+
LDAP_SERVER_IP =
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
##########################################################################
|
|
73
|
+
# MySQL access configuration
|
|
74
|
+
##########################################################################
|
|
75
|
+
|
|
76
|
+
[mysql]
|
|
77
|
+
|
|
78
|
+
# A comma delimited list of users to ignore
|
|
79
|
+
ignore_users = root
|
|
80
|
+
|
|
81
|
+
# Connection parameters
|
|
82
|
+
host = localhost
|
|
83
|
+
db = mysql
|
|
84
|
+
user = root
|
|
85
|
+
password =
|
|
86
|
+
auth_plugin = mysql_native_password
|
|
87
|
+
ssl_ca =
|
|
88
|
+
ssl_cert =
|
|
89
|
+
ssl_key =
|
|
90
|
+
ssl_verify_identity =
|
|
91
|
+
ssl_verify_cert =
|
|
92
|
+
use_pure =
|
|
93
|
+
SERVICE_NAME =
|
|
94
|
+
|
|
95
|
+
##########################################################################
|
|
96
|
+
# General configuration
|
|
97
|
+
##########################################################################
|
|
98
|
+
|
|
99
|
+
[general]
|
|
100
|
+
|
|
101
|
+
# Authentication plugin (one of simple, sasl or pam)
|
|
102
|
+
auth_plugin = pam
|
|
103
|
+
|
|
104
|
+
# Oracle-compatible PAM
|
|
105
|
+
compat = false
|
|
106
|
+
|
|
107
|
+
# Database management system - one of mysql, psms (Percona Server for MySQL) or mariadb.
|
|
108
|
+
dbms = psms
|
|
109
|
+
|
|
110
|
+
# Add LDAP users to MySQL if they don't exist, or ignore them?
|
|
111
|
+
add_ldap_users_to_mysql = true
|
|
112
|
+
|
|
113
|
+
# Remove MySQL users if they don't exist in LDAP, or ignore them?
|
|
114
|
+
remove_users_from_mysql = true
|
|
115
|
+
|
|
116
|
+
# Database for privileges granting. If empty privileges will be granted for everything i.e. *.*.
|
|
117
|
+
# Wildcard could be used like "db%" so grants will be applied for every database with "db" name prefix.
|
|
118
|
+
database =
|
|
119
|
+
|
|
120
|
+
# Privileges to grant to users in MySQL.
|
|
121
|
+
# Note these privileges are only applied to users when created. Existing users are unaffected.
|
|
122
|
+
user_privilege_all = false
|
|
123
|
+
user_privilege_create = false
|
|
124
|
+
user_privilege_create_role = false
|
|
125
|
+
user_privilege_alter = false
|
|
126
|
+
user_privilege_create_user = false
|
|
127
|
+
user_privilege_alter_rename = false
|
|
128
|
+
user_privilege_event = false
|
|
129
|
+
user_privilege_execute = false
|
|
130
|
+
user_privilege_trigger = false
|
|
131
|
+
user_privilege_insert = false
|
|
132
|
+
user_privilege_update = false
|
|
133
|
+
user_privilege_delete = false
|
|
134
|
+
user_privilege_drop = false
|
|
135
|
+
|
|
136
|
+
# A comma delimited list of roles to grant membership to when creating
|
|
137
|
+
# users. Note that existing users will not be modified. Roles specified
|
|
138
|
+
# here must already exist in MySQL. Supported in MySQL 8+ and MariaDB 10+.
|
|
139
|
+
roles_to_grant = group1,group2
|
|
140
|
+
|
|
141
|
+
# A comma delimited list of roles to grant membership to with ADMIN when
|
|
142
|
+
# creating users. Note that existing users will not be modified. Roles
|
|
143
|
+
# specified here must already exist in MySQL. Supported in MySQL 8+ and MariaDB 10+.
|
|
144
|
+
roles_to_grant_with_admin = group3
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
##########################################################################
|
|
2
|
+
# This config file is read before the user config and is present to
|
|
3
|
+
# supply default values for optional parameters.
|
|
4
|
+
##########################################################################
|
|
5
|
+
|
|
6
|
+
[ldap]
|
|
7
|
+
debug = false
|
|
8
|
+
server_uri =
|
|
9
|
+
bind_username =
|
|
10
|
+
bind_password =
|
|
11
|
+
filter_string = (objectClass=user)
|
|
12
|
+
username_attribute = sAMAccountName
|
|
13
|
+
use_starttls = false
|
|
14
|
+
ca_cert_file =
|
|
15
|
+
cert_file =
|
|
16
|
+
key_file =
|
|
17
|
+
search_scope = LEVEL
|
|
18
|
+
base_dn =
|
|
19
|
+
admin_base_dn =
|
|
20
|
+
admin_filter_string =
|
|
21
|
+
use_kerberos =
|
|
22
|
+
SERVICE_NAME =
|
|
23
|
+
LDAP_SERVER_IP =
|
|
24
|
+
ignore_users =
|
|
25
|
+
|
|
26
|
+
[mysql]
|
|
27
|
+
ignore_users = root
|
|
28
|
+
host = localhost
|
|
29
|
+
db = mysql
|
|
30
|
+
user = root
|
|
31
|
+
password =
|
|
32
|
+
auth_plugin = mysql_native_password
|
|
33
|
+
ssl_ca =
|
|
34
|
+
ssl_cert =
|
|
35
|
+
ssl_key =
|
|
36
|
+
ssl_verify_identity =
|
|
37
|
+
ssl_verify_cert =
|
|
38
|
+
use_pure =
|
|
39
|
+
SERVICE_NAME =
|
|
40
|
+
|
|
41
|
+
[general]
|
|
42
|
+
auth_plugin = pam
|
|
43
|
+
compat = false
|
|
44
|
+
dbms = psms
|
|
45
|
+
add_ldap_users_to_mysql = true
|
|
46
|
+
remove_users_from_mysql = true
|
|
47
|
+
database =
|
|
48
|
+
user_privilege_all = false
|
|
49
|
+
user_privilege_create = false
|
|
50
|
+
user_privilege_create_role = false
|
|
51
|
+
user_privilege_alter = false
|
|
52
|
+
user_privilege_create_user = false
|
|
53
|
+
user_privilege_alter_rename = false
|
|
54
|
+
user_privilege_event = false
|
|
55
|
+
user_privilege_execute = false
|
|
56
|
+
user_privilege_trigger = false
|
|
57
|
+
user_privilege_insert = false
|
|
58
|
+
user_privilege_update = false
|
|
59
|
+
user_privilege_delete = false
|
|
60
|
+
user_privilege_drop = false
|
|
61
|
+
roles_to_grant =
|
|
62
|
+
roles_to_grant_with_admin =
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
8
|
+
|
|
9
|
+
"""LDAP connection functions."""
|
|
10
|
+
|
|
11
|
+
# FIX THIS!
|
|
12
|
+
# pylint: disable=too-many-branches
|
|
13
|
+
|
|
14
|
+
import ssl
|
|
15
|
+
import sys
|
|
16
|
+
|
|
17
|
+
from ldap3 import Connection, Server, Tls, SASL, KERBEROS
|
|
18
|
+
from ldap3.core.exceptions import LDAPBindError, LDAPSocketOpenError, \
|
|
19
|
+
LDAPStartTLSError, LDAPSASLBindInProgressError, LDAPSASLPrepError, \
|
|
20
|
+
LDAPSASLMechanismNotSupportedError
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
from urllib.parse import urlparse
|
|
25
|
+
except ImportError:
|
|
26
|
+
from urlparse import urlparse
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def connect_ldap_server(config):
|
|
30
|
+
"""Setup the connection to the LDAP server.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
config (ConfigParser): The application configuration
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
ldap3.core.connection.Connection: The LDAP connection object
|
|
37
|
+
"""
|
|
38
|
+
# Parse the server URI
|
|
39
|
+
uri = urlparse(config.get('ldap', 'server_uri'))
|
|
40
|
+
|
|
41
|
+
# Create the TLS configuration object if required
|
|
42
|
+
tls = None
|
|
43
|
+
|
|
44
|
+
if uri.scheme == 'ldaps' or config.getboolean('ldap', 'use_starttls'):
|
|
45
|
+
|
|
46
|
+
ca_cert_file = None
|
|
47
|
+
if config.get('ldap', 'ca_cert_file') != '':
|
|
48
|
+
ca_cert_file = config.get('ldap', 'ca_cert_file')
|
|
49
|
+
|
|
50
|
+
cert_file = None
|
|
51
|
+
if config.get('ldap', 'cert_file') != '':
|
|
52
|
+
cert_file = config.get('ldap', 'cert_file')
|
|
53
|
+
|
|
54
|
+
key_file = None
|
|
55
|
+
if config.get('ldap', 'key_file') != '':
|
|
56
|
+
key_file = config.get('ldap', 'key_file')
|
|
57
|
+
|
|
58
|
+
tls = Tls(
|
|
59
|
+
local_private_key_file=key_file,
|
|
60
|
+
local_certificate_file=cert_file,
|
|
61
|
+
validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1,
|
|
62
|
+
ca_certs_file=ca_cert_file)
|
|
63
|
+
|
|
64
|
+
# Debug
|
|
65
|
+
if config.getboolean('ldap', 'debug'):
|
|
66
|
+
sys.stderr.write("TLS/SSL configuration: %s\n" % tls)
|
|
67
|
+
|
|
68
|
+
# Create the server object
|
|
69
|
+
server = Server(uri.hostname,
|
|
70
|
+
port=uri.port,
|
|
71
|
+
tls=tls,
|
|
72
|
+
use_ssl=uri.scheme == 'ldaps')
|
|
73
|
+
|
|
74
|
+
# Debug
|
|
75
|
+
if config.getboolean('ldap', 'debug'):
|
|
76
|
+
sys.stderr.write("LDAP server config: %s\n" % server)
|
|
77
|
+
|
|
78
|
+
# Create the connection
|
|
79
|
+
conn = None
|
|
80
|
+
try:
|
|
81
|
+
if config.get('ldap', 'bind_username') == '' and config.get('ldap', 'use_krb') == '':
|
|
82
|
+
conn = Connection(server, auto_bind=True)
|
|
83
|
+
elif config.get('ldap', 'use_krb') != '':
|
|
84
|
+
if config.get('ldap', 'SERVICE_NAME') != '' and config.get('ldap', 'LDAP_SERVER_IP') != '':
|
|
85
|
+
SERVICE_NAME = config.get('ldap', 'SERVICE_NAME')
|
|
86
|
+
LDAP_SERVER_IP = config.get('ldap', 'LDAP_SERVER_IP')
|
|
87
|
+
SPN = f"{SERVICE_NAME}/{LDAP_SERVER_IP}"
|
|
88
|
+
conn = Connection(server, user=SPN, authentication=SASL, sasl_mechanism=KERBEROS)
|
|
89
|
+
else:
|
|
90
|
+
conn = Connection(server, authentication=SASL, sasl_mechanism=KERBEROS)
|
|
91
|
+
else:
|
|
92
|
+
conn = Connection(server,
|
|
93
|
+
config.get('ldap', 'bind_username'),
|
|
94
|
+
config.get('ldap', 'bind_password'),
|
|
95
|
+
auto_bind=True)
|
|
96
|
+
except LDAPSocketOpenError as exception:
|
|
97
|
+
sys.stderr.write("Error connecting to the LDAP server: %s\n" %
|
|
98
|
+
exception)
|
|
99
|
+
except LDAPBindError as exception:
|
|
100
|
+
sys.stderr.write("Error binding to the LDAP server: %s\n" % exception)
|
|
101
|
+
|
|
102
|
+
except LDAPSASLBindInProgressError as exception:
|
|
103
|
+
sys.stderr.write("SASL bind in progress error: %s\n" % exception)
|
|
104
|
+
|
|
105
|
+
except LDAPSASLPrepError as exception:
|
|
106
|
+
sys.stderr.write("SASL prep error: %s\n" % exception)
|
|
107
|
+
|
|
108
|
+
except LDAPSASLMechanismNotSupportedError as exception:
|
|
109
|
+
sys.stderr.write("SASL mechanism not supported error: %s\n" % exception)
|
|
110
|
+
|
|
111
|
+
# Debug
|
|
112
|
+
if config.getboolean('ldap', 'debug'):
|
|
113
|
+
sys.stderr.write("Initial LDAP connection: %s\n" % conn)
|
|
114
|
+
|
|
115
|
+
# Enable TLS if STARTTLS is configured
|
|
116
|
+
if uri.scheme != 'ldaps' and config.getboolean('ldap', 'use_starttls'):
|
|
117
|
+
try:
|
|
118
|
+
conn.start_tls()
|
|
119
|
+
except LDAPStartTLSError as exception:
|
|
120
|
+
sys.stderr.write("Error starting TLS: %s\n" % exception)
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
# Debug
|
|
124
|
+
if config.getboolean('ldap', 'debug'):
|
|
125
|
+
sys.stderr.write("Final LDAP connection: %s\n" % conn)
|
|
126
|
+
|
|
127
|
+
return conn
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
8
|
+
|
|
9
|
+
"""LDAP user functions."""
|
|
10
|
+
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
from ldap3.core.exceptions import LDAPInvalidFilterError, \
|
|
14
|
+
LDAPInvalidScopeError, LDAPAttributeError
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_ldap_users(config, conn, admin):
|
|
18
|
+
"""Get a list of users from the LDAP server.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
config (ConfigParser): The application configuration
|
|
22
|
+
conn (ldap3.core.connection.Connection): The LDAP connection object
|
|
23
|
+
admin (bool): Return users in the admin group?
|
|
24
|
+
Returns:
|
|
25
|
+
str[]: A list of user names
|
|
26
|
+
"""
|
|
27
|
+
users = []
|
|
28
|
+
|
|
29
|
+
if admin:
|
|
30
|
+
base_dn = config.get('ldap', 'admin_base_dn')
|
|
31
|
+
search_filter = config.get('ldap', 'admin_filter_string')
|
|
32
|
+
else:
|
|
33
|
+
base_dn = config.get('ldap', 'base_dn')
|
|
34
|
+
search_filter = config.get('ldap', 'filter_string')
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
conn.search(base_dn,
|
|
38
|
+
search_filter,
|
|
39
|
+
config.get('ldap', 'search_scope'),
|
|
40
|
+
attributes=[config.get('ldap', 'username_attribute')]
|
|
41
|
+
)
|
|
42
|
+
except LDAPInvalidScopeError as exception:
|
|
43
|
+
sys.stderr.write("Error searching the LDAP directory: %s\n" % exception)
|
|
44
|
+
sys.exit(1)
|
|
45
|
+
except LDAPAttributeError as exception:
|
|
46
|
+
sys.stderr.write("Error searching the LDAP directory: %s\n" % exception)
|
|
47
|
+
sys.exit(1)
|
|
48
|
+
except LDAPInvalidFilterError as exception:
|
|
49
|
+
sys.stderr.write("Error searching the LDAP directory: %s\n" % exception)
|
|
50
|
+
sys.exit(1)
|
|
51
|
+
|
|
52
|
+
for entry in conn.entries:
|
|
53
|
+
users.append(entry[config.get('ldap', 'username_attribute')].value)
|
|
54
|
+
|
|
55
|
+
return users
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_filtered_ldap_users(config, conn, admin):
|
|
59
|
+
"""Get a filtered list of users from the LDAP server, having removed users
|
|
60
|
+
to be ignored.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
config (ConfigParser): The application configuration
|
|
64
|
+
conn (ldap3.core.connection.Connection): The LDAP connection object
|
|
65
|
+
admin (bool): Return users in the admin group?
|
|
66
|
+
Returns:
|
|
67
|
+
str[]: A filtered list of user names
|
|
68
|
+
"""
|
|
69
|
+
users = get_ldap_users(config, conn, admin)
|
|
70
|
+
if users is None:
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
# Remove ignored users
|
|
74
|
+
for user in config.get('ldap', 'ignore_users').split(','):
|
|
75
|
+
try:
|
|
76
|
+
users.remove(user)
|
|
77
|
+
except ValueError:
|
|
78
|
+
pass
|
|
79
|
+
|
|
80
|
+
return users
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
8
|
+
|
|
9
|
+
"""MySQL user functions."""
|
|
10
|
+
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
import mysql.connector
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_my_users(conn):
|
|
17
|
+
"""Get a list of user from the MySQL server.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
conn (connection): The MySQL connection object
|
|
21
|
+
Returns:
|
|
22
|
+
str[]: A list of user names
|
|
23
|
+
"""
|
|
24
|
+
cur = conn.cursor()
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
cur.execute("SELECT user FROM mysql.user\n"
|
|
28
|
+
"WHERE account_locked='N'\n"
|
|
29
|
+
"AND password_expired='N'\n"
|
|
30
|
+
"AND authentication_string IS NOT NULL;")
|
|
31
|
+
rows = cur.fetchall()
|
|
32
|
+
except mysql.connector.Error as exception:
|
|
33
|
+
sys.stderr.write("Error retrieving MySQL users: %s\n" %
|
|
34
|
+
exception)
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
users = []
|
|
38
|
+
|
|
39
|
+
for row in rows:
|
|
40
|
+
users.append(row[0])
|
|
41
|
+
|
|
42
|
+
cur.close()
|
|
43
|
+
|
|
44
|
+
return users
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def get_filtered_my_users(config, conn):
|
|
48
|
+
"""Get a filtered list of users from the MySQL server, having
|
|
49
|
+
removed users to be ignored.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
config (ConfigParser): The application configuration
|
|
53
|
+
conn (connection): The MySQL connection object
|
|
54
|
+
Returns:
|
|
55
|
+
str[]: A filtered list of users
|
|
56
|
+
"""
|
|
57
|
+
users = get_my_users(conn)
|
|
58
|
+
if users is None:
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
# Remove ignored users
|
|
62
|
+
for user in config.get('mysql', 'ignore_users').split(','):
|
|
63
|
+
try:
|
|
64
|
+
users = [members for members in users if user != members]
|
|
65
|
+
except ValueError:
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
return users
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_user_privileges(config, user, with_admin=False):
|
|
72
|
+
"""Generate a list of user privileges to use when creating users
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
config (ConfigParser): The application configuration
|
|
76
|
+
user (str): The user name to be granted privileges
|
|
77
|
+
with_admin (bool): Generate a list of privileges that will have the WITH
|
|
78
|
+
GRANT OPTION specified, if True
|
|
79
|
+
Returns:
|
|
80
|
+
str: A SQL snippet listing the user privileges
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
privilege_list = ''
|
|
84
|
+
sql = ''
|
|
85
|
+
|
|
86
|
+
if config.get('general', 'database') != '':
|
|
87
|
+
database = config.get('general', 'database')
|
|
88
|
+
database = '`' + database + '`'
|
|
89
|
+
else:
|
|
90
|
+
database = '*'
|
|
91
|
+
|
|
92
|
+
if config.getboolean('general', 'user_privilege_all') or with_admin:
|
|
93
|
+
privilege_list = privilege_list + 'ALL' + ', '
|
|
94
|
+
else:
|
|
95
|
+
if config.getboolean('general', 'user_privilege_create'):
|
|
96
|
+
privilege_list = privilege_list + 'CREATE'+ ', '
|
|
97
|
+
|
|
98
|
+
if config.getboolean('general', 'user_privilege_create_role'):
|
|
99
|
+
privilege_list = privilege_list + 'CREATE ROLE'+ ', '
|
|
100
|
+
|
|
101
|
+
if config.getboolean('general', 'user_privilege_alter'):
|
|
102
|
+
privilege_list = privilege_list + 'CREATE, INSERT, ALTER'+ ', '
|
|
103
|
+
|
|
104
|
+
if config.getboolean('general', 'user_privilege_create_user'):
|
|
105
|
+
privilege_list = privilege_list + 'CREATE USER'+ ', '
|
|
106
|
+
|
|
107
|
+
if config.getboolean('general', 'user_privilege_alter_rename'):
|
|
108
|
+
privilege_list = privilege_list + 'CREATE, INSERT, ALTER, DROP'+ ', '
|
|
109
|
+
|
|
110
|
+
if config.getboolean('general', 'user_privilege_event'):
|
|
111
|
+
privilege_list = privilege_list + 'EVENT'+ ', '
|
|
112
|
+
|
|
113
|
+
if config.getboolean('general', 'user_privilege_execute'):
|
|
114
|
+
privilege_list = privilege_list + 'EXECUTE'+ ', '
|
|
115
|
+
|
|
116
|
+
if config.getboolean('general', 'user_privilege_trigger'):
|
|
117
|
+
privilege_list = privilege_list + 'TRIGGER'+ ', '
|
|
118
|
+
|
|
119
|
+
if config.getboolean('general', 'user_privilege_insert'):
|
|
120
|
+
privilege_list = privilege_list + 'INSERT'+ ', '
|
|
121
|
+
|
|
122
|
+
if config.getboolean('general', 'user_privilege_update'):
|
|
123
|
+
privilege_list = privilege_list + 'UPDATE'+ ', '
|
|
124
|
+
|
|
125
|
+
if config.getboolean('general', 'user_privilege_delete'):
|
|
126
|
+
privilege_list = privilege_list + 'DELETE'+ ', '
|
|
127
|
+
|
|
128
|
+
if config.getboolean('general', 'user_privilege_drop'):
|
|
129
|
+
privilege_list = privilege_list + 'DROP'+ ', '
|
|
130
|
+
|
|
131
|
+
if privilege_list.endswith(', '):
|
|
132
|
+
privilege_list = privilege_list[:-2]
|
|
133
|
+
|
|
134
|
+
if privilege_list != '':
|
|
135
|
+
sql = 'GRANT %s ON %s.* TO "%s"' % (privilege_list, database, user)
|
|
136
|
+
if with_admin:
|
|
137
|
+
sql = sql + " WITH GRANT OPTION"
|
|
138
|
+
sql = sql + ';'
|
|
139
|
+
|
|
140
|
+
return sql
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def get_user_grants(config, user, with_admin=False):
|
|
144
|
+
"""Get a SQL string to GRANT membership to the configured roles when
|
|
145
|
+
creating a new user.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
config (ConfigParser): The application configuration
|
|
149
|
+
user (str): The user name to be granted roles
|
|
150
|
+
with_admin (bool): Generate a list of roles that will have the WITH
|
|
151
|
+
ADMIN OPTION specified, if True
|
|
152
|
+
Returns:
|
|
153
|
+
str: A SQL snippet listing the user roles
|
|
154
|
+
"""
|
|
155
|
+
roles = ''
|
|
156
|
+
sql = ''
|
|
157
|
+
|
|
158
|
+
if with_admin:
|
|
159
|
+
roles_to_grant = config.get('general',
|
|
160
|
+
'roles_to_grant_with_admin').split(',')
|
|
161
|
+
else:
|
|
162
|
+
roles_to_grant = config.get('general', 'roles_to_grant').split(',')
|
|
163
|
+
|
|
164
|
+
if roles_to_grant != ['']:
|
|
165
|
+
for role_to_grant in roles_to_grant:
|
|
166
|
+
roles = roles + '"' + role_to_grant + '", '
|
|
167
|
+
|
|
168
|
+
if roles.endswith(', '):
|
|
169
|
+
roles = roles[:-2]
|
|
170
|
+
|
|
171
|
+
if roles != '':
|
|
172
|
+
sql = 'GRANT %s TO "%s"' % (roles, user)
|
|
173
|
+
|
|
174
|
+
if with_admin:
|
|
175
|
+
sql = sql + " WITH ADMIN OPTION"
|
|
176
|
+
|
|
177
|
+
sql = sql + ';'
|
|
178
|
+
|
|
179
|
+
return sql
|
|
180
|
+
|
|
181
|
+
def get_create_users(ldap_users, my_users):
|
|
182
|
+
"""Get a filtered list of users to create.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
ldap_users (str[]): A list of users in LDAP
|
|
186
|
+
my_users (str[]): A list of users in MySQL
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
str[]: A list of users that exist in LDAP but not in MySQL
|
|
190
|
+
"""
|
|
191
|
+
users = []
|
|
192
|
+
|
|
193
|
+
for user in ldap_users:
|
|
194
|
+
if user not in my_users:
|
|
195
|
+
users.append(user)
|
|
196
|
+
|
|
197
|
+
return users
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def get_drop_users(ldap_users, my_users):
|
|
201
|
+
"""Get a filtered list of users to drop.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
ldap_users (str[]): A list of users in LDAP
|
|
205
|
+
my_users (str[]): A list of users in MySQL
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
str[]: A list of users that exist in MySQL but not in LDAP
|
|
209
|
+
"""
|
|
210
|
+
users = []
|
|
211
|
+
|
|
212
|
+
for user in my_users:
|
|
213
|
+
if user not in ldap_users:
|
|
214
|
+
users.append(user)
|
|
215
|
+
|
|
216
|
+
return users
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: myldapsync
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: Synchronise LDAP users to MySQL
|
|
5
|
+
Home-page: https://github.com/6eh01der/myldapsync
|
|
6
|
+
Author: Artur Lebedev
|
|
7
|
+
Author-email: ras_atari@mail.ru
|
|
8
|
+
License: MySQL
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Requires-Python: >=3.7
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: ldap3>=2.9.1
|
|
21
|
+
Requires-Dist: mysql-connector-python>=8.0.31
|
|
22
|
+
Requires-Dist: pyasn1>=0.4.8
|
|
23
|
+
|
|
24
|
+
# myldapsync - fork of [pgldapsync](https://github.com/EnterpriseDB/pgldapsync) by [EnterpriseDB Corporation](https://www.enterprisedb.com/) adopted for MySQL and MariaDB
|
|
25
|
+
|
|
26
|
+
This Python module allows you to synchronise MySQL or MariaDB users
|
|
27
|
+
with users in an LDAP directory.
|
|
28
|
+
|
|
29
|
+
Require MySQL 8 with installed and configured simple or sasl LDAP Authentication Plugin or PAM plugin (pam also supported in MySQL 5.7 and MariaDB).
|
|
30
|
+
|
|
31
|
+
Percona Server for MySQL:
|
|
32
|
+
|
|
33
|
+
https://docs.percona.com/percona-server/8.0/ldap-authentication
|
|
34
|
+
|
|
35
|
+
https://docs.percona.com/percona-server/8.0/pam-plugin.html
|
|
36
|
+
|
|
37
|
+
MySQL:
|
|
38
|
+
|
|
39
|
+
https://dev.mysql.com/doc/refman/8.0/en/ldap-pluggable-authentication.html
|
|
40
|
+
|
|
41
|
+
https://dev.mysql.com/doc/refman/8.0/en/pam-pluggable-authentication.html
|
|
42
|
+
|
|
43
|
+
MariaDB:
|
|
44
|
+
|
|
45
|
+
https://mariadb.com/kb/en/authentication-plugin-pam/
|
|
46
|
+
|
|
47
|
+
*myldapsync is supported on Python 3.7 or later.*
|
|
48
|
+
|
|
49
|
+
In order to use it, you will need to create a _config.ini_
|
|
50
|
+
file containing the site-specific configuration you require.
|
|
51
|
+
See _config.ini.example_ for a complete list of all the
|
|
52
|
+
available configuration options. This file should be copied to
|
|
53
|
+
create your own configuration.
|
|
54
|
+
|
|
55
|
+
Once configured, simply run myldapsync like so:
|
|
56
|
+
|
|
57
|
+
python3 myldapsync.py /path/to/config.ini
|
|
58
|
+
|
|
59
|
+
In order to test the configuration (and dump the SQL that would
|
|
60
|
+
be executed to stdout), run it like this:
|
|
61
|
+
|
|
62
|
+
python3 myldapsync.py --dry-run /path/to/config.ini
|
|
63
|
+
|
|
64
|
+
## Creating a virtual environment for dev/test
|
|
65
|
+
|
|
66
|
+
python3 -m venv /path/to/myldapsync
|
|
67
|
+
source /path/to/myldapsync/bin/activate
|
|
68
|
+
pip install -r requirements.txt
|
|
69
|
+
|
|
70
|
+
Adapt the first command as required for your environment/Python
|
|
71
|
+
version.
|
|
72
|
+
|
|
73
|
+
## Creating a package
|
|
74
|
+
|
|
75
|
+
To create a package (wheel), run the following in your virtual
|
|
76
|
+
environment:
|
|
77
|
+
|
|
78
|
+
python3 setup.py sdist bdist_wheel --universal
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
requirements.txt
|
|
5
|
+
setup.py
|
|
6
|
+
myldapsync/__init__.py
|
|
7
|
+
myldapsync/config.ini.example
|
|
8
|
+
myldapsync/config_default.ini
|
|
9
|
+
myldapsync.egg-info/PKG-INFO
|
|
10
|
+
myldapsync.egg-info/SOURCES.txt
|
|
11
|
+
myldapsync.egg-info/dependency_links.txt
|
|
12
|
+
myldapsync.egg-info/entry_points.txt
|
|
13
|
+
myldapsync.egg-info/requires.txt
|
|
14
|
+
myldapsync.egg-info/top_level.txt
|
|
15
|
+
myldapsync/ldaputils/__init__.py
|
|
16
|
+
myldapsync/ldaputils/connection.py
|
|
17
|
+
myldapsync/ldaputils/users.py
|
|
18
|
+
myldapsync/myutils/__init__.py
|
|
19
|
+
myldapsync/myutils/users.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
myldapsync
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# myldapsync - adopted for MySQL fork of pgldapsync by EnterpriseDB Corporation
|
|
4
|
+
#
|
|
5
|
+
# Synchronise MySQL users with users in an LDAP directory.
|
|
6
|
+
#
|
|
7
|
+
###############################################################################
|
|
8
|
+
|
|
9
|
+
"""myldapsync package creation."""
|
|
10
|
+
|
|
11
|
+
import setuptools
|
|
12
|
+
|
|
13
|
+
with open('README.md', 'r', encoding='utf-8') as fh:
|
|
14
|
+
long_description = fh.read()
|
|
15
|
+
|
|
16
|
+
# Get the requirements list for the current version of Python
|
|
17
|
+
with open('requirements.txt', 'r', encoding='utf-8') as reqf:
|
|
18
|
+
required = reqf.read().splitlines()
|
|
19
|
+
|
|
20
|
+
setuptools.setup(
|
|
21
|
+
name="myldapsync",
|
|
22
|
+
version="1.0.2",
|
|
23
|
+
author="Artur Lebedev",
|
|
24
|
+
author_email="ras_atari@mail.ru",
|
|
25
|
+
description="Synchronise LDAP users to MySQL",
|
|
26
|
+
license='MySQL',
|
|
27
|
+
long_description=long_description,
|
|
28
|
+
long_description_content_type="text/markdown",
|
|
29
|
+
url="https://github.com/6eh01der/myldapsync",
|
|
30
|
+
packages=setuptools.find_packages(),
|
|
31
|
+
install_requires=required,
|
|
32
|
+
python_requires='>=3.7',
|
|
33
|
+
classifiers=[
|
|
34
|
+
'Programming Language :: Python :: 3',
|
|
35
|
+
'Programming Language :: Python :: 3.7',
|
|
36
|
+
'Programming Language :: Python :: 3.8',
|
|
37
|
+
'Programming Language :: Python :: 3.9',
|
|
38
|
+
'Programming Language :: Python :: 3.10',
|
|
39
|
+
'Programming Language :: Python :: 3.11',
|
|
40
|
+
"License :: OSI Approved :: GNU General Public License (GPL)",
|
|
41
|
+
"Operating System :: OS Independent",
|
|
42
|
+
],
|
|
43
|
+
entry_points={
|
|
44
|
+
'console_scripts': ['myldapsync=myldapsync.__init__:main'],
|
|
45
|
+
},
|
|
46
|
+
include_package_data=True
|
|
47
|
+
)
|