-
Notifications
You must be signed in to change notification settings - Fork 0
/
webappLatest.py
1349 lines (1151 loc) · 50.7 KB
/
webappLatest.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, redirect, url_for, flash, session, send_from_directory, abort, jsonify
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from flask_session import Session
from flask_caching import Cache
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
from cryptography.fernet import Fernet, InvalidToken
import mysql.connector
from functools import wraps
import os
import json
from crontab import CronTab
import uuid
import subprocess
from datetime import datetime, timedelta
from flask_bcrypt import Bcrypt
from werkzeug.security import generate_password_hash, check_password_hash # Import password hashing function
import logging
import logging.handlers
import pyotp
#from routes.widgets import widgets_bp
#from routes.widgets import widget_type, widget_device, widget_jobs, widget_site
from routes.widgets import fetch_widgets_data
from routes.dev_import import upload
from routes.smtp_config import smtp_config_ui, update_smtp, send_email
from routes.abx_setup import setup1, setup2, setup3, setup4
from routes.emailer import send_registration_email
from config.config import CONFIG
app = Flask(__name__)
app.config['CACHE_TYPE'] = 'simple'
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_PERMANENT'] = False # Session will expire when the browser is closed
app.config['SESSION_USE_SIGNER'] = True # Session data is signed for security
app.config['SESSION_KEY_PREFIX'] = CONFIG['FlaskSession']['prefix'] # Replace with your own prefix
app.secret_key = CONFIG['FlaskSession']['key'] # Change this to a strong, random value
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+mysqlconnector://{CONFIG['Database']['username']}:{CONFIG['Database']['password']}@{CONFIG['Database']['host']}/{CONFIG['Database']['database']}" # Replace with your database URL
db = SQLAlchemy(app)
cache = Cache(app)
# Register the widgets blueprint
#app.register_blueprint(widgets_bp, url_prefix='/widgets_type')
# Import routes after creating the Flask app and SQLAlchemy instance
#from routes import widgets
# Register the route function with Flask
class BackupRecord(db.Model):
__tablename__ = 'backup_records' # Specify the table name if different from the class name
id = db.Column(db.Integer, primary_key=True)
backup_date = db.Column(db.DateTime)
device_name = db.Column(db.String(255))
site_name = db.Column(db.String(255))
type = db.Column(db.String(255)) # Adjust the data type and length as per your schema
username = db.Column(db.String(255)) # Adjust the data type and length as per your schema
exit_status = db.Column(db.String(50)) # Adjust the data type and length as per your schema
file_name = db.Column(db.String(255)) # Adjust the data type and length as per your schema
class DeviceRecord(db.Model):
__tablename__ = 'passwords'
username = db.Column(db.String(255))
device = db.Column(db.String(255), primary_key=True, unique=True)
encrypted_password = db.Column(db.String(255))
group_name = db.Column(db.String(255))
type = db.Column(db.String(255))
Session(app)
# Initialize Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# Initialize Flask-Bcrypt for password hashing
bcrypt = Bcrypt(app)
# Function to check database connection
def check_db_connection():
if os.path.exists('setup.lock'):
with app.app_context():
try:
# Attempt to connect to the database
result = db.session.execute(text('SELECT 1'))
return True
except:
return False
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'csv'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Middleware to store the client IP in the thread-local storage
@app.before_request
def store_client_ip():
request.client_ip = request.remote_addr
# Register routes using add_url_rule()
#app.add_url_rule('/widgets_device', 'widget_device', widget_device)
app.add_url_rule('/smtp_config_ui', 'smtp_config_ui', smtp_config_ui)
app.add_url_rule('/update_smtp', 'update_smtp', update_smtp, methods=['POST'])
app.add_url_rule('/send_email', 'send_email', send_email, methods=['POST'])
app.add_url_rule('/fetch_widgets_data', 'fetch_widgets_data', fetch_widgets_data)
app.add_url_rule('/upload', 'upload', upload, methods=['POST'])
app.add_url_rule('/setup1', 'setup1', setup1, methods=['POST', 'GET'])
app.add_url_rule('/setup2', 'setup2', setup2, methods=['POST', 'GET'])
app.add_url_rule('/setup3', 'setup3', setup3, methods=['POST', 'GET'])
app.add_url_rule('/setup4', 'setup4', setup4, methods=['POST', 'GET'])
# Configure logging
custom_log_file = 'AirBackupX_messages.log' # Specify the log file path
# Create a custom log formatter
custom_logger = logging.getLogger('custom_logger')
custom_logger.setLevel(logging.INFO) # Set the desired logging level for custom logs (e.g., INFO)
# Define the log format for your custom logs (includes IP info)
custom_log_format = '[%(asctime)s] [%(ip)s] [%(levelname)s]: %(message)s'
# Configure a custom log handler for your custom logger
custom_log_handler = logging.handlers.TimedRotatingFileHandler(
custom_log_file,
when="midnight",
interval=1,
backupCount=7
)
custom_log_handler.setFormatter(logging.Formatter(custom_log_format))
# Set the custom log handler's filter to include IP information
class IPLogFilter(logging.Filter):
def filter(self, record):
record.ip = request.client_ip
return True
custom_log_handler.addFilter(IPLogFilter())
# Add the custom log handler to the custom logger
custom_logger.addHandler(custom_log_handler)
# Define the User class for Flask-Login
class User(UserMixin):
def __init__(self, id, username, password_hash):
self.id = id
self.username = username
self.password_hash = password_hash
def set_password(self, password):
self.password_hash = generate_password_hash(password).decode('utf-8')
def check_password(self, password):
return check_password_hash(self.password_hash, password)
# @app.before_request
# def check_session_timeout():
# # Get the current session's last access time
# last_access_time = session.get('last_access_time')
# if last_access_time is not None:
# # Calculate the elapsed time since the last access
# elapsed_time = datetime.now() - last_access_time
# # Set your desired session timeout duration (e.g., 30 minutes)
# session_timeout_duration = timedelta(minutes=5)
# if elapsed_time > session_timeout_duration:
# # Session has expired, clear the session and redirect to the login page
# session.clear()
# flash('Your session has expired due to inactivity.', 'info')
# return redirect(url_for('login'))
# # Update the last access time for the session
# session['last_access_time'] = datetime.now()
@app.before_request
def check_session_timeout():
# Exclude certain routes from session timeout check
excluded_routes = ['login', 'logout'] # Add more routes if needed
if request.endpoint and request.endpoint not in excluded_routes:
last_access_time = session.get('last_access_time')
if last_access_time is not None:
elapsed_time = datetime.now() - last_access_time
session_timeout_duration = timedelta(minutes=5)
if elapsed_time > session_timeout_duration:
session.clear()
flash('Your session has expired due to inactivity.', 'info')
return redirect(url_for('login'))
# Update the last access time for the session
session['last_access_time'] = datetime.now()
@login_manager.user_loader
def load_user(username):
# Load a user from the database based on the provided user_id
# Replace this code with your actual database query logic
cursor.execute('SELECT id, username, password_hash FROM users WHERE id = %s', (username,))
result = cursor.fetchone()
if result:
user_id, username, password_hash = result
# Create a User object with the fetched data
user = User(user_id, username, password_hash)
return user
# Return None if user_id is not found in the database
return None
def requires_admin(view_func):
@wraps(view_func)
def wrapped(*args, **kwargs):
user_id = current_user.username
# Check if the user's role is in the cache
role = cache.get(f'user_role:{user_id}')
if role is None:
# If not in cache, query the database and store the result in the cache
# Connect to MySQL/MariaDB database
db_config = {
'host': CONFIG['Database']['host'],
'port': CONFIG['Database']['port'],
'user': CONFIG['Database']['username'],
'password': CONFIG['Database']['password'],
'database': CONFIG['Database']['database']
}
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute('SELECT role FROM users WHERE username = %s', (user_id,))
role = cursor.fetchone()
#role = get_user_role(user_id) # Replace with your database query function
cache.set(f'user_role:{user_id}', role, timeout=3600) # Cache for 1 hour (adjust timeout as needed)
# Close the cursor and connection after the operations are done
cursor.close()
conn.close()
if role and role[0] == 'admin':
return view_func(*args, **kwargs)
else:
# Redirect to a different route or show an access denied message
flash('Unauthorized Access', 'error')
return redirect(url_for('dashboard'))
return wrapped
@app.route('/profile')
@login_required
def profile():
return render_template('profile.html', user=current_user, email=CONFIG['Admin']['emailID'])
@app.route('/user_registration', methods=['GET', 'POST'])
@login_required
@requires_admin
def user_registration():
# # Check if the current user's username is "2222"
# if current_user.username == "2222":
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
emailID = request.form['emailID']
role = request.form['role']
confirm_password = request.form['confirm_password']
# Check if the username and password are provided
if not username or not password or not confirm_password:
flash('Both username and password are required.', 'error')
return redirect(url_for('user_registration.html'))
# Check if the passwords match
if password != confirm_password:
flash('Passwords do not match. Please enter the same password twice.', 'error')
return render_template('user_registration.html')
# Check if the username already exists in the database
cursor.execute('SELECT id FROM users WHERE username = %s', (username,))
existing_user = cursor.fetchone()
if existing_user:
flash('Username already exists. Please choose a different username.', 'error')
else:
# Generate a random OTP secret
totp_secret = pyotp.random_base32()
# Hash and store the user's password
password_hash = generate_password_hash(password)
cursor.execute('INSERT INTO users (username, password_hash, emailID, role, totp_secret) VALUES (%s, %s, %s, %s, %s)', (username, password_hash, emailID, role, totp_secret))
conn.commit()
send_registration_email(username=username, email=emailID, totp_secret=totp_secret)
flash('Registration successful! Account Created', 'success')
# If 'next' is provided in the query string, redirect there, otherwise go to 'dashboard'
#next_page = request.args.get('next', None)
return redirect(url_for('dashboard'))
#return redirect(url_for('login'))
return render_template('user_registration.html')
# else:
# flash('Unauthorized Access', 'error')
# return redirect(url_for('dashboard'))
@app.route('/reset_password', methods=['POST'])
@login_required
def reset_password():
if request.method == 'POST':
new_password = request.form['new_password']
confirm_password = request.form['confirm_password']
# Check if the new password and new confirm password are provided
if not new_password or not confirm_password:
flash('Both new password and confirm password are required.', 'error')
return redirect(url_for('profile'))
# Check if the new password and confirm password match
if new_password == confirm_password:
# Update the user's password in the database
new_password_hash = generate_password_hash(new_password)
cursor.execute('UPDATE users SET password_hash = %s WHERE id = %s', (new_password_hash, current_user.id))
conn.commit()
flash('Password Reset successful! You can now log in with new password.', 'success')
return redirect(url_for('logout'))
else:
flash('Password Reset failed', 'error')
return redirect(url_for('profile'))
@app.route('/', methods=['GET', 'POST'])
def login():
if os.path.exists('setup.lock'):
# Handle login logic
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
otp = request.form['otp']
# Check if the username and password are provided
if not username or not password or not otp:
flash('Both username, password and OTP are required.', 'error')
return redirect(url_for('login'))
# Query the database to retrieve the user's hashed password
cursor.execute('SELECT id, username, password_hash, totp_secret FROM users WHERE username = %s', (username,))
result = cursor.fetchone()
if result:
totp = pyotp.TOTP(result[3])
if totp.verify(otp):
if check_password_hash(result[2], password):
# If the username and password are valid, log in the user
user = User(result[0], result[1], result[2])
login_user(user)
# Initialize the session and set the last access time
session['last_access_time'] = datetime.now()
flash('Login successful!', 'success')
custom_logger.info(f'User {username} logged in Successfully')
# Redirect the user to the stored 'next' URL or '/dashboard' if it doesn't exist
#next_url = request.args.get('next', url_for('dashboard'))
#return redirect(next_url)
#next_page = request.args.get('next')
#return render_template('login.html', next_page=next_page)
return redirect(url_for('dashboard'))
else:
flash('Login failed. Please check your credentials.', 'error')
return render_template('login.html')
else:
flash('Invalid OTP. Please enter a valid OTP.', 'error')
return render_template('login.html')
else:
flash('Account Does Not Exist. Please check your credentials.', 'error')
return render_template('login.html')
else:
return render_template('login.html')
else:
return render_template('setup1.html')
#@app.route('/', methods=['GET', 'POST'])
#def login():
# if request.method == 'POST':
# username = request.form['username']\
# password = request.form['password']
#
# # Check if the username and password are provided
# if not username or not password:
# flash('Both username and password are required.', 'error')
# return redirect(url_for('login'))
#
# # Query the database to retrieve the user's hashed password
# cursor.execute('SELECT id, username, password_hash FROM users WHERE username = %s', (username,))
# result = cursor.fetchone()
#
# if result and check_password_hash(result[2], password):
# # If the username and password are valid, log in the user
# user = User(result[0], result[1], result[2])
# login_user(user)
#
# # Initialize the session and set the last access time
# session['last_access_time'] = datetime.now()
#
# flash('Login successful!', 'success')
# custom_logger.info(f'User {username} logged in Successfully')
# # Redirect the user to the stored 'next' URL or '/dashboard' if it doesn't exist
# #next_url = request.args.get('next', url_for('dashboard'))
# #return redirect(next_url)
# #next_page = request.args.get('next')
# #return render_template('login.html', next_page=next_page)
# return redirect(url_for('dashboard'))
#
# flash('Login failed. Please check your credentials.', 'error')
#
# # Capture the 'next' query parameter if it exists
# #next_page = request.args.get('next')
#
# #if next_page:
# # Store 'next' in the session for later use
# # session['next'] = next_page
#
# return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out.', 'success')
return redirect(url_for('login'))
#return redirect(url_for('login', flash_message='You have been logged out.'))
# Define your local backup directory here
local_directory = CONFIG['Datastore']['BackupPath'] # Replace with the path to your local directory
@app.route('/explorer')
def explorer():
contents = list_directory_contents(local_directory)
return render_template('explorer.html', folder_path=local_directory, contents=contents)
@app.route('/download/<path:file_path>')
def download_file(file_path):
full_file_path = os.path.join(local_directory, file_path)
# Check if the file exists
if os.path.exists(full_file_path):
try:
# Use send_from_directory to serve the file as an attachment
return send_from_directory(local_directory, file_path, as_attachment=True)
except Exception as e:
return f"Error downloading file: {str(e)}"
else:
abort(404)
@app.route('/explore/<path:folder_path>')
def explore_directory(folder_path):
full_folder_path = os.path.join(local_directory, folder_path)
contents = list_directory_contents(full_folder_path)
return render_template('explorer.html', folder_path=folder_path, contents=contents)
@app.route('/get_contents/<path:folder_path>')
def get_contents(folder_path):
full_folder_path = os.path.join(local_directory, folder_path)
contents = list_directory_contents(full_folder_path)
return jsonify(contents)
def list_directory_contents(directory_path):
try:
contents = []
for item in os.listdir(directory_path):
full_item_path = os.path.join(directory_path, item)
is_directory = os.path.isdir(full_item_path)
timestamp = get_timestamp(full_item_path)
relative_path = os.path.relpath(full_item_path, local_directory)
contents.append((relative_path, is_directory, timestamp))
return contents
except Exception as e:
return [str(e)]
def get_timestamp(file_path):
try:
timestamp = os.path.getmtime(file_path)
return timestamp
except Exception as e:
return None
# Function to generate or load the encryption key
def get_or_generate_key():
key_file = 'encryption_key.key'
if os.path.exists(key_file):
with open(key_file, 'rb') as file:
key = file.read()
else:
key = Fernet.generate_key()
with open(key_file, 'wb') as file:
file.write(key)
return key
# Initialize Fernet cipher with the key
encryption_key = get_or_generate_key()
cipher_suite = Fernet(encryption_key)
# Connect to MySQL/MariaDB database
db_config = {
'host': CONFIG['Database']['host'],
'port': CONFIG['Database']['port'],
'user': CONFIG['Database']['username'],
'password': CONFIG['Database']['password'],
'database': CONFIG['Database']['database']
}
if check_db_connection():
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
# # Create the 'passwords' table if it doesn't exist
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS passwords (
# username VARCHAR(255),
# device VARCHAR(255) UNIQUE,
# encrypted_password BLOB
# )
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS smtp_config (
# id INT PRIMARY KEY AUTO_INCREMENT,
# smtp_server VARCHAR(255),
# smtp_port INT,
# username VARCHAR(255) UNIQUE,
# encrypted_password BLOB
# );
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS groups (
# id INT AUTO_INCREMENT PRIMARY KEY,
# name VARCHAR(255) UNIQUE
# )
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS types (
# id INT AUTO_INCREMENT PRIMARY KEY,
# name VARCHAR(255) UNIQUE
# )
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS users (
# id INT AUTO_INCREMENT PRIMARY KEY,
# username VARCHAR(255) UNIQUE NOT NULL,
# password_hash VARCHAR(255) NOT NULL
# )
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS cron_jobs (
# job_id VARCHAR(255) PRIMARY KEY,
# site_name VARCHAR(255),
# script_path VARCHAR(255),
# minute VARCHAR(10),
# hour VARCHAR(10),
# day VARCHAR(10),
# month VARCHAR(10),
# day_of_week VARCHAR(10)
# )
# ''')
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS backup_records (
# id INT AUTO_INCREMENT PRIMARY KEY,
# backup_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
# device_name VARCHAR(255) NOT NULL,
# site_name VARCHAR(255) NOT NULL,
# type VARCHAR(255) NOT NULL,
# username VARCHAR(255) NOT NULL,
# exit_status ENUM('failed', 'succeeded') NOT NULL,
# file_name VARCHAR(255) NOT NULL
# )
# ''')
# #cursor.execute('''
# # ALTER TABLE passwords
# # ADD COLUMN type VARCHAR(255)
# #''')
# conn.commit()
# Function to encrypt a password
def encrypt_password(password):
encrypted_password = cipher_suite.encrypt(password.encode())
return encrypted_password
# Function to decrypt a password
def decrypt_password(encrypted_password):
try:
decrypted_password = cipher_suite.decrypt(encrypted_password).decode()
return decrypted_password
except InvalidToken:
return "Invalid token (possibly corrupted or tampered data)"
# Function to list available device names
def list_devices():
# cursor = None
# try:
# conn = mysql.connector.connect(**db_config)
# cursor = conn.cursor()
# cursor.execute('SELECT device FROM passwords')
# results = cursor.fetchall()
# devices = [result[0] for result in results]
# return devices
# except Exception as e:
# # Handle exceptions, log errors, etc.
# return "error"
# finally:
# if cursor:
# cursor.close()
# if conn and conn.is_connected():
# conn.close()
#def list_devices():
devices = DeviceRecord.query.all()
return [device.device for device in devices]
# Function to list available groups
def list_groups():
cursor.execute('SELECT name FROM groups')
results = cursor.fetchall()
groups = [result[0] for result in results]
return groups
# Function to list available types
def list_types():
cursor.execute('SELECT name FROM types')
results = cursor.fetchall()
types = [result[0] for result in results]
return types
# Define routes and views
#@app.route('/')
#def login():
# return render_template('login.html')
@app.route('/dashboard')
@login_required
def dashboard():
devices = list_devices()
groups = list_groups()
types = list_types()
return render_template('dashboard.html', devices=devices, groups=groups, types=types)
@app.route('/devicemgmt')
@login_required
def devicemgmt():
devices = list_devices()
groups = list_groups()
types = list_types()
return render_template('devicemgmt.html', devices=devices, groups=groups, types=types)
@app.route('/sitemgmt')
@login_required
def sitemgmt():
devices = list_devices()
groups = list_groups()
types = list_types()
return render_template('sitemgmt.html', devices=devices, groups=groups, types=types)
@app.route('/typemgmt')
@login_required
def typemgmt():
devices = list_devices()
groups = list_groups()
types = list_types()
return render_template('typemgmt.html', devices=devices, groups=groups, types=types)
@app.route('/credretrieve')
@login_required
def credretrieve():
return render_template('credretrieve.html')
@app.route('/create', methods=['POST'])
@login_required
@requires_admin
def create():
if request.method == 'POST':
username = request.form['username']
device = request.form['device']
password = request.form['password']
selected_group = request.form['group']
selected_type = request.form['type']
# Check if any form field is empty
if not username or not device or not password:
flash("Please fill in all fields.", 'error')
else:
# Check if the device name already exists
cursor.execute('SELECT device FROM passwords WHERE device = %s', (device,))
result = cursor.fetchone()
if result:
flash(f"Device '{device}' already exists. Please choose a different device name.", 'error')
else:
encrypted_password = encrypt_password(password)
try:
cursor.execute('INSERT INTO passwords (username, device, encrypted_password, group_name, type ) VALUES (%s, %s, %s, %s, %s)',
(username, device, encrypted_password, selected_group, selected_type))
conn.commit()
flash("Device created successfully!", 'success')
except mysql.connector.IntegrityError as e:
if e.errno == 1062:
flash(f"Device '{device}' already exists. Please choose a different device name.", 'error')
else:
flash(f"An error occurred: {e}", 'error')
return redirect(url_for('dashboard'))
@app.route('/retrieve', methods=['POST', 'GET'])
@login_required
@requires_admin
def retrieve():
if request.method == 'POST':
device = request.form['device']
# Check if the device name is empty
if not device:
flash("Please enter a device name.", 'error')
else:
cursor.execute('SELECT username, encrypted_password FROM passwords WHERE device = %s', (device,))
result = cursor.fetchone()
if result:
username, encrypted_password = result
decrypted_password = decrypt_password(encrypted_password)
return render_template('retrieve.html', username=username, password=decrypted_password)
else:
flash(f"No credentials found for device {device}.", 'error')
return render_template('retrieve.html', username=None, password=None)
# Function to edit credentials by device name
@app.route('/edit/<device>', methods=['POST', 'GET'])
@requires_admin
@login_required
def edit(device):
cursor.execute('SELECT username FROM passwords WHERE device = %s', (device,))
result = cursor.fetchone()
if result:
old_username = result[0]
if request.method == 'POST':
new_username = request.form['new_username']
new_password = request.form['new_password']
if not new_username or not new_password:
flash("Please fill in all fields.", 'error')
else:
encrypted_password = encrypt_password(new_password)
try:
cursor.execute('UPDATE passwords SET username = %s, encrypted_password = %s WHERE device = %s',
(new_username, encrypted_password, device))
conn.commit()
flash("Credentials updated successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash(f"No credentials found for device {device}.", 'error')
return redirect(url_for('dashboard'))
return render_template('edit.html', device=device, old_username=old_username)
# Function to delete credentials by device name
@app.route('/delete/<device>', methods=['GET'])
@requires_admin
@login_required
def delete(device):
try:
cursor.execute('DELETE FROM passwords WHERE device = %s', (device,))
conn.commit()
flash(f"Device '{device}' deleted successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
return redirect(url_for('dashboard'))
@app.route('/create_group', methods=['POST'])
@login_required
def create_group():
if request.method == 'POST':
group_name = request.form['group_name']
if group_name:
try:
# Check if the group already exists
cursor.execute('SELECT id FROM groups WHERE name = %s', (group_name,))
existing_group = cursor.fetchone()
if existing_group:
flash(f"Group '{group_name}' already exists. Please choose a different group name.", 'error')
else:
cursor.execute('INSERT INTO groups (name) VALUES (%s)', (group_name,))
conn.commit()
flash(f"Group '{group_name}' created successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash("Please enter a group name.", 'error')
return redirect(url_for('dashboard'))
@app.route('/delete_group', methods=['GET', 'POST'])
@requires_admin
@login_required
def delete_group():
groups = list_groups()
if request.method == 'POST':
# Handle group deletion here
group_to_delete = request.form.get('delete_group')
if group_to_delete:
# Add code to delete the selected group from the database
try:
cursor.execute('DELETE FROM groups WHERE name = %s', (group_to_delete,))
conn.commit()
flash(f"Group '{group_to_delete}' deleted successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash("Please select a group to delete.", 'error')
return redirect(url_for('dashboard'))
@app.route('/update_device_group/<device>', methods=['POST'])
@login_required
def update_device_group(device):
if request.method == 'POST':
group_id = request.form['group_id']
if group_id:
try:
cursor.execute('UPDATE devices SET group_id = %s WHERE name = %s', (group_id, device))
conn.commit()
flash(f"Group updated successfully for device '{device}'!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash("Please select a group.", 'error')
return redirect(url_for('dashboard'))
@app.route('/create_type', methods=['POST'])
@login_required
def create_type():
if request.method == 'POST':
type_name = request.form['type_name']
if type_name:
try:
# Check if the group already exists
cursor.execute('SELECT id FROM types WHERE name = %s', (type_name,))
existing_type = cursor.fetchone()
if existing_type:
flash(f"type '{type_name}' already exists. Please choose a different type name.", 'error')
else:
cursor.execute('INSERT INTO types (name) VALUES (%s)', (type_name,))
conn.commit()
flash(f"type '{type_name}' created successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash("Please enter a type name.", 'error')
return redirect(url_for('dashboard'))
@app.route('/delete_type', methods=['GET', 'POST'])
@requires_admin
@login_required
def delete_type():
types = list_types()
if request.method == 'POST':
# Handle group deletion here
type_to_delete = request.form.get('delete_type')
if type_to_delete:
# Add code to delete the selected group from the database
try:
cursor.execute('DELETE FROM types WHERE name = %s', (type_to_delete,))
conn.commit()
flash(f"Type '{type_to_delete}' deleted successfully!", 'success')
except Exception as e:
flash(f"An error occurred: {e}", 'error')
else:
flash("Please select a type to delete.", 'error')
return redirect(url_for('dashboard'))
# Function to create a valid cron schedule string
def create_cron_schedule(minute, hour, day, month, day_of_week):
cron_string = f"{minute} {hour} {day} {month} {day_of_week}"
return cron_string
@app.route('/schedule_cron_job', methods=['GET', 'POST'])
@login_required
def schedule_cron_job():
if request.method == 'POST':
site_name = request.form.get('site_name')
script_file = f"/Users/ram/Downloads/AirBackupX/scripts/{site_name}.py"
# Ensure that the parent directory exists, create it if it doesn't
parent_directory = os.path.dirname(script_file)
if not os.path.exists(parent_directory):
os.makedirs(parent_directory)
# Check if the script file exists, and create it with content if it doesn't
if not os.path.exists(script_file):
# Create the script file and add your desired content
with open(script_file, 'w') as file:
file.write("BackupCodeContentGoesHereblablaablaaaa\n")
minute = request.form.get('minute')
hour = request.form.get('hour')
day = request.form.get('day')
month = request.form.get('month')
day_of_week = request.form.get('day_of_week')
# Create a valid cron schedule string
cron_schedule = create_cron_schedule(minute, hour, day, month, day_of_week)
# Generate a unique job ID (e.g., using UUID)
job_id = str(uuid.uuid4())
# Initialize a CronTab object
cron = CronTab(user='ram') # Replace 'your_username' with the appropriate username
# Create a new cron job and set its command
job = cron.new(command=f'python {script_file}')
# Set the cron schedule using the cron_schedule string
job.setall(cron_schedule)
# Write the cron job to the user's crontab
cron.write()
# Store the cron job and associated data in the cron_jobs dictionary
cursor.execute('''
INSERT INTO cron_jobs (job_id, site_name, script_path, minute, hour, day, month, day_of_week)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
''', (job_id, site_name, script_file, minute, hour, day, month, day_of_week))
conn.commit()
flash('Cron job scheduled successfully!', 'success')
return redirect(url_for('dashboard'))
return render_template('schedule_cron_job.html')
@app.route('/list_cron_jobs', methods=['GET'])
@login_required
def list_cron_jobs():
cursor.execute('SELECT * FROM cron_jobs')
cron_job_data = cursor.fetchall()
return render_template('list_cron_jobs.html', cron_job_data=cron_job_data)
@app.route('/edit_cron_job/<job_id>', methods=['GET', 'POST'])
@login_required
def edit_cron_job(job_id):
if request.method == 'POST':
cursor.execute('SELECT script_path FROM cron_jobs WHERE job_id = %s', (job_id,))
result = cursor.fetchone()
if result:
script_path = result[0]
# Remove the cron job from the user's crontab
cron = CronTab(user='ram') # Replace 'your_username' with the appropriate username
jobs = cron.find_command(script_path)
for job in jobs:
cron.remove(job)
cron.write()
#Fetch Form data to update
minute = request.form.get('minute')
hour = request.form.get('hour')
day = request.form.get('day')
month = request.form.get('month')
day_of_week = request.form.get('day_of_week')
# Create a valid cron schedule string
cron_schedule = create_cron_schedule(minute, hour, day, month, day_of_week)
# Create a new cron job and set its command
job = cron.new(command=f'python {script_path}')
# Set the cron schedule using the cron_schedule string
job.setall(cron_schedule)
# Write the cron job to the user's crontab
cron.write()
#Update the cron job details in the database
cursor.execute('''
UPDATE cron_jobs
SET minute = %s, hour = %s, day = %s, month = %s, day_of_week = %s
WHERE job_id = %s
''', (minute, hour, day, month, day_of_week, job_id))
conn.commit()
flash('Cron job updated successfully!', 'success')