我想用Python(2.6或2.7)/C代码对用户进行身份验证。

我必须创建一个简单的身份验证模块,它将username and password作为输入并验证用户(例如PAM UNIX 模块)。

有办法吗?

最佳答案

您是否正在寻找类似 LogonUser 的东西来认证本地用户,例如:

#!python
# -*- coding: utf-8 -*-

from os import environ
from ctypes import windll, byref, POINTER, FormatError, GetLastError
from ctypes.wintypes import (
  LPCWSTR, WCHAR, DWORD, HANDLE, BOOL
)

MAX_COMPUTERNAME_LENGTH = 15
LOGON32_LOGON_NETWORK_CLEARTEXT = 8
LOGON32_PROVIDER_DEFAULT = 0

CloseHandle = windll.Kernel32.CloseHandle
CloseHandle.argtypes = [HANDLE]
CloseHandle.restype = BOOL

LogonUser = windll.Advapi32.LogonUserW
LogonUser.argtypes = [LPCWSTR, LPCWSTR, LPCWSTR, DWORD, DWORD, POINTER(HANDLE)]
LogonUser.restype = BOOL

if __name__ == '__main__':
  username = 'MyUsername'
  password = 'MyPassword'

  token = HANDLE()
  status = LogonUser(username,
                     environ['COMPUTERNAME'],
                     password,
                     LOGON32_LOGON_NETWORK_CLEARTEXT,
                     LOGON32_PROVIDER_DEFAULT,
                     byref(token))
  error = GetLastError()

  if status:
    print('OK, successes')
    CloseHandle(token)
  else:
    print(FormatError(error))

09-04 00:24