From 4d3d0d4701479de6473cee0787fdf0cfd0a4d658 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Fri, 5 Jan 2018 02:41:56 +0100 Subject: [PATCH] Added config file and session auth --- .gitignore | 5 ++++- README.md | 10 ++++++++++ compair.py | 18 ++++++++---------- config_template.py | 10 ++++++++++ pam.py | 25 +++++++++++++++++++------ 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 README.md create mode 100644 config_template.py diff --git a/.gitignore b/.gitignore index 10f2e64..92f9881 100644 --- a/.gitignore +++ b/.gitignore @@ -100,5 +100,8 @@ ENV/ # mypy .mypy_cache/ -# Ignore generated models +# generated models /models + +# config file +config.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..f33eced --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Ubuntu Howdy + +Windows Helloâ„¢ style authentication for Ubuntu + +Notes: + +tail /var/log/auth.log + +/etc/pam.d/sudo +auth sufficient pam_python.so /path/to/pam.py diff --git a/compair.py b/compair.py index feb16a0..5da05b3 100644 --- a/compair.py +++ b/compair.py @@ -3,13 +3,12 @@ import cv2 import sys import os +import config + def stop(status): video_capture.release() sys.exit(status) -path = "" -distance = 3 - try: if not isinstance(sys.argv[1], str): sys.exit(1) @@ -17,19 +16,18 @@ except IndexError: sys.exit(1) user = sys.argv[1] - # Get a reference to webcam #0 (the default one) -video_capture = cv2.VideoCapture(1) +video_capture = cv2.VideoCapture(config.device_id) encodings = [] try: for exposure in ["L", "M", "S"]: - ref = face_recognition.load_image_file(path + "/" + user + "/" + exposure + ".jpg") + ref = face_recognition.load_image_file(os.path.dirname(__file__) + "/models/" + user + "/" + exposure + ".jpg") enc = face_recognition.face_encodings(ref)[0] encodings.append(enc) except FileNotFoundError: - stop(802) + stop(10) tries = 0 @@ -44,10 +42,10 @@ while True: matches = face_recognition.face_distance(encodings, face_encoding) for match in matches: - if match < distance: + if match < config.certainty: stop(0) - if tries => 100: - stop(801) + if tries > config.frame_count: + stop(11) tries += 1 diff --git a/config_template.py b/config_template.py new file mode 100644 index 0000000..8b6c65a --- /dev/null +++ b/config_template.py @@ -0,0 +1,10 @@ +# The certainty of the detected face belonging to the user of the account +# On a scale from 1 to 10, values above 5 are not recomended +certainty = 3 + +# The number of frames to capture and to process before timing out +frame_count = 120 + +# The /dev/videoX id to capture frames from +# On my laptop, video0 is the normal camera and video1 is the IR version +device_id = 1 diff --git a/pam.py b/pam.py index fd2a11c..9018c36 100644 --- a/pam.py +++ b/pam.py @@ -2,17 +2,30 @@ import subprocess import sys import os -def pam_sm_authenticate(pamh, flags, args): - status = subprocess.call(["python3", "compair.py", pamh.get_user()]) +def doAuth(pamh): + status = subprocess.call(["python3", "/compair.py", pamh.get_user()]) - if status == 801: - print("Timeout reached, ould not find a known face") + if status == 10: + print("No face model is known for this user, aborting") return pamh.PAM_SYSTEM_ERR - if status == 34: - print("No face model is known for this user") + if status == 11: + print("Timeout reached, ould not find a known face") return pamh.PAM_SYSTEM_ERR if status == 0: print("Identified face as " + os.environ.get("USER")) return pamh.PAM_SUCCESS + print(status) return pamh.PAM_SYSTEM_ERR + +def pam_sm_authenticate(pamh, flags, args): + return doAuth(pamh) + +def pam_sm_open_session(pamh, flags, args): + return doAuth(pamh) + +def pam_sm_close_session(pamh, flags, argv): + return pamh.PAM_SUCCESS + +def pam_sm_setcred(pamh, flags, argv): + return pamh.PAM_SUCCESS