Upload 2 files
Browse files- Dockerfile +22 -0
- gradio_app.py +69 -0
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Build Stage
|
| 2 |
+
FROM diegogslomp/samba-ad-dc AS builder
|
| 3 |
+
|
| 4 |
+
# Install Python and required libraries for Gradio
|
| 5 |
+
RUN apt-get update && \
|
| 6 |
+
apt-get install -y python3 python3-pip python3-dev libsasl2-dev libldap2-dev libssl-dev && \
|
| 7 |
+
pip3 install gradio ldap3
|
| 8 |
+
|
| 9 |
+
# Stage 2: Final Image
|
| 10 |
+
FROM diegogslomp/samba-ad-dc
|
| 11 |
+
|
| 12 |
+
# Copy the configuration files from the build stage
|
| 13 |
+
COPY --from=builder /etc/samba/smb.conf /etc/samba/smb.conf
|
| 14 |
+
|
| 15 |
+
# Copy Gradio app
|
| 16 |
+
COPY ./gradio_app.py /app/gradio_app.py
|
| 17 |
+
|
| 18 |
+
# Expose port 7860 for the Gradio app
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
# Start Samba AD and Gradio app
|
| 22 |
+
CMD ["/bin/bash", "-c", "samba-ad-dc -F -S & python3 /app/gradio_app.py"]
|
gradio_app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ldap3 import Server, Connection, ALL, MODIFY_ADD
|
| 3 |
+
|
| 4 |
+
# Connect to Samba AD via LDAP
|
| 5 |
+
def ldap_connect():
|
| 6 |
+
# Replace with your Samba AD server details
|
| 7 |
+
server = Server('ldap://localhost', get_info=ALL)
|
| 8 |
+
conn = Connection(server, user='administrator@EXAMPLE.COM', password='adminpassword')
|
| 9 |
+
if not conn.bind():
|
| 10 |
+
return None
|
| 11 |
+
return conn
|
| 12 |
+
|
| 13 |
+
# Fetch all users from the LDAP server
|
| 14 |
+
def get_users():
|
| 15 |
+
conn = ldap_connect()
|
| 16 |
+
if conn:
|
| 17 |
+
conn.search('dc=example,dc=com', '(objectClass=user)', attributes=['cn', 'mail'])
|
| 18 |
+
users = []
|
| 19 |
+
for entry in conn.entries:
|
| 20 |
+
users.append(f"Name: {entry['cn'][0]}, Email: {entry.get('mail', ['No email'])[0]}")
|
| 21 |
+
conn.unbind()
|
| 22 |
+
return "\n".join(users) if users else "No users found."
|
| 23 |
+
return "Failed to connect to LDAP."
|
| 24 |
+
|
| 25 |
+
# Add a new user to Samba AD
|
| 26 |
+
def add_user(username, email):
|
| 27 |
+
conn = ldap_connect()
|
| 28 |
+
if conn:
|
| 29 |
+
# Construct the DN for the new user
|
| 30 |
+
dn = f"CN={username},CN=Users,DC=example,DC=com"
|
| 31 |
+
user_attributes = {
|
| 32 |
+
'objectClass': ['top', 'person', 'organizationalPerson', 'user'],
|
| 33 |
+
'sAMAccountName': username,
|
| 34 |
+
'userPrincipalName': f'{username}@example.com',
|
| 35 |
+
'mail': email,
|
| 36 |
+
'givenName': username,
|
| 37 |
+
'sn': username,
|
| 38 |
+
'displayName': username
|
| 39 |
+
}
|
| 40 |
+
conn.add(dn, attributes=user_attributes)
|
| 41 |
+
if conn.result['result'] == 0:
|
| 42 |
+
conn.unbind()
|
| 43 |
+
return f"User {username} added successfully!"
|
| 44 |
+
conn.unbind()
|
| 45 |
+
return "Failed to add user."
|
| 46 |
+
return "Failed to connect to LDAP."
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
def display_users():
|
| 50 |
+
return get_users()
|
| 51 |
+
|
| 52 |
+
def add_new_user(username, email):
|
| 53 |
+
return add_user(username, email)
|
| 54 |
+
|
| 55 |
+
# Create Gradio Interface
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("### Samba AD User Management")
|
| 58 |
+
|
| 59 |
+
# Display users
|
| 60 |
+
with gr.Row():
|
| 61 |
+
gr.Button("Fetch Users").click(display_users, outputs=gr.Textbox())
|
| 62 |
+
|
| 63 |
+
# Add new user
|
| 64 |
+
with gr.Row():
|
| 65 |
+
username_input = gr.Textbox(label="Username")
|
| 66 |
+
email_input = gr.Textbox(label="Email")
|
| 67 |
+
gr.Button("Add User").click(add_new_user, inputs=[username_input, email_input], outputs=gr.Textbox())
|
| 68 |
+
|
| 69 |
+
gr.Interface(fn=display_users, inputs=[], outputs="text").launch(server_name="0.0.0.0", server_port=7860)
|