[SOLVED] Package to enable Touch ID with sudo on macOS

Questions about WAPT Packaging / Requests and help regarding Wapt packages.
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Answer
bastien30
Messages: 38
Registration: March 8, 2024 - 3:21 PM

November 28, 2024 - 2:09 PM

Good morning,

Here is the package I made to be able to validate sudo requests with Touch ID on macOS.

I based my work on this article: https://www.macg.co/macos/2023/08/macos ... -id-138763

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *
import re

template_file = r'/etc/pam.d/sudo_local.template'
target_file = r'/etc/pam.d/sudo_local'

enabled_pattern = r'^auth\ *sufficient\ *pam\_tid\.so$'
disabled_pattern = r'^#auth\ *sufficient\ *pam\_tid\.so$'

def is_sudo_tid_enabled():
    if not isfile(target_file):
        error(r'File %s does  not exists !' % target_file)
    else:
        with open(target_file) as f:
            for line in f:
                if re.match(enabled_pattern, line):
                    return True
                elif re.match(disabled_pattern, line):
                    return False
    return "NOTMATCHING"

def enable_sudo_tid():
    file_to_rewrite = []
    with open(target_file) as f:
        for line in f:
            if re.match(disabled_pattern, line):
                file_to_rewrite.append(line[1:])
            else:
                file_to_rewrite.append(line)
    with open(target_file, r'w') as f:
        f.writelines(file_to_rewrite)

def disable_sudo_tid():
    file_to_rewrite = []
    with open(target_file) as f:
        for line in f:
            if re.match(enabled_pattern, line):
                file_to_rewrite.append(r'#' + line)
            else:
                file_to_rewrite.append(line)
    with open(target_file, r'w') as f:
        f.writelines(file_to_rewrite)

def install():
    if not isfile(target_file):
        filecopyto(template_file, target_file)

    # Check if already enabled
    check = is_sudo_tid_enabled()
    if check == r'NOTMATCHING':
        error(r'Error matching pattern in %s file' % target_file)
    else:
        if check:
            print(r'TouchID support for sudo is already enabled.')
        else:
            print(r'Enabling TouchID support for sudo...')
            enable_sudo_tid()

def uninstall():
    print(r'Disabling TouchID support for sudo...')
    disable_sudo_tid()
Answer