Thread: [pywin32-checkins] pywin32/win32/src win32security.i,1.11,1.12
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: <mha...@us...> - 2003-10-23 01:43:17
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1:/tmp/cvs-serv10473 Modified Files: win32security.i Log Message: New LSA functions from Roger, and support for a LSA_HANDLE object Index: win32security.i =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32security.i,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** win32security.i 1 Sep 2003 12:09:50 -0000 1.11 --- win32security.i 23 Oct 2003 00:56:35 -0000 1.12 *************** *** 49,52 **** --- 49,102 ---- } + %{ + #undef PyHANDLE + #include "PyWinObjects.h" + // Support for LSAHandle objects. Like a PyHANDLE, but calls LsaClose + class PyLSA_HANDLE: public PyHANDLE + { + public: + PyLSA_HANDLE(HANDLE hInit) : PyHANDLE(hInit) {} + virtual BOOL Close(void) { + NTSTATUS err = m_handle ? LsaClose((LSA_HANDLE)m_handle) : STATUS_SUCCESS; + m_handle = 0; + if (err!= STATUS_SUCCESS) + PyWin_SetAPIError("LsaClose", LsaNtStatusToWinError(err)); + return err== STATUS_SUCCESS; + } + virtual const char *GetTypeName() { + return "PyLSA_HANDLE"; + } + }; + + BOOL PyWinObject_AsLSA_HANDLE(PyObject *ob, LSA_HANDLE *pRes, BOOL bNoneOK = FALSE); + PyObject *PyWinObject_FromLSA_HANDLE(LSA_HANDLE h) + { + return new PyLSA_HANDLE(h); + } + + BOOL PyWinObject_CloseLSA_HANDLE(PyObject *obHandle) + { + BOOL ok; + if (PyHANDLE_Check(obHandle)) + // Python error already set. + ok = ((PyLSA_HANDLE *)obHandle)->Close(); + else if PyInt_Check(obHandle) { + NTSTATUS err; + err=LsaClose((HKEY)PyInt_AsLong(obHandle)); + ok = err == STATUS_SUCCESS; + if (!ok) + PyWin_SetAPIError("LsaClose",LsaNtStatusToWinError(err)); + } else { + PyErr_SetString(PyExc_TypeError, "A handle must be a LSA_HANDLE object or an integer"); + ok = FALSE; + } + return ok; + } + + // And re-define, so PyHANDLE in function sigs gets the PyHANDLE treatment. + #define PyHANDLE HANDLE + + %} + // @object PyTOKEN_PRIVILEGES|An object representing Win32 token privileges. // @comm This is a sequence (eg, list) of (id, attributes) *************** *** 87,93 **** TCHAR *ret_string = NULL; USHORT strlen = 0; ! if (!PyWinObject_AsTCHAR(obstr, &ret_string, bNoneOk)) return FALSE; ! strlen = wcslen(ret_string); plsaus->Buffer = ret_string; plsaus->Length = strlen * sizeof(WCHAR); --- 137,148 ---- TCHAR *ret_string = NULL; USHORT strlen = 0; ! if (!PyWinObject_AsTCHAR(obstr, &ret_string, bNoneOk)) { ! plsaus->Buffer = ret_string; return FALSE; ! } ! if (ret_string == NULL) ! strlen=0; ! else ! strlen = wcslen(ret_string); plsaus->Buffer = ret_string; plsaus->Length = strlen * sizeof(WCHAR); *************** *** 199,202 **** --- 254,259 ---- static BOOL (WINAPI *csdtssd)(PSECURITY_DESCRIPTOR,DWORD,SECURITY_INFORMATION, LPTSTR*,PULONG) = NULL; static BOOL (WINAPI *cssdtsd)(LPCTSTR,DWORD,PSECURITY_DESCRIPTOR*,PULONG) = NULL; + static long (WINAPI *lsarpcn)(POLICY_NOTIFICATION_INFORMATION_CLASS,HANDLE) = NULL; + static long (WINAPI *lsaupcn)(POLICY_NOTIFICATION_INFORMATION_CLASS,HANDLE) = NULL; BOOL CheckIfSupported(char *funcname, WCHAR *dllname, FARPROC *fp) *************** *** 286,289 **** --- 343,349 ---- FARPROC fp=NULL; + // Load the Secur32.dll library to CheckIfSupported's GetModuleHandle will work. + // If we fail here, GetModuleHandle will too, so no need to check result here. + HMODULE hMod = LoadLibrary(_T("Secur32.dll")); if (CheckIfSupported("ConvertSidToStringSidW",_T("Advapi32.dll"),&fp)) cstss= (BOOL (WINAPI *)(PSID, WCHAR **))(fp); *************** *** 294,297 **** --- 354,362 ---- if (CheckIfSupported("ConvertStringSecurityDescriptorToSecurityDescriptorW",_T("Advapi32.dll"),&fp)) cssdtsd=(BOOL (WINAPI *)(LPCTSTR,DWORD,PSECURITY_DESCRIPTOR*,PULONG))(fp); + if (CheckIfSupported("LsaRegisterPolicyChangeNotification",_T("Secur32.dll"),&fp)) + lsarpcn=(NTSTATUS (NTAPI *)(POLICY_NOTIFICATION_INFORMATION_CLASS,HANDLE))(fp); + if (CheckIfSupported("LsaUnregisterPolicyChangeNotification",_T("Secur32.dll"),&fp)) + lsaupcn=(NTSTATUS (NTAPI *)(POLICY_NOTIFICATION_INFORMATION_CLASS,HANDLE))(fp); + %} *************** *** 1332,1339 **** %} ! // @pyswig <o PyHandle>|GetPolicyHandle|Opens a policy handle for the specified system ! %native(GetPolicyHandle) PyGetPolicyHandle; %{ ! static PyObject *PyGetPolicyHandle(PyObject *self, PyObject *args) { PyObject *obsystem_name = NULL; --- 1397,1409 ---- %} ! // we used to expose this as "GetPolicyHandle". It has been renamed ! // to "LsaOpenPolicy" to be consistent with win32, but GetPolicyHandle still ! // exists as an alias. ! %native(GetPolicyHandle) PyLsaOpenPolicy; ! ! // @pyswig <o PyHandle>|LsaOpenPolicy|Opens a policy handle for the specified system ! %native(LsaOpenPolicy) PyLsaOpenPolicy; %{ ! static PyObject *PyLsaOpenPolicy(PyObject *self, PyObject *args) { PyObject *obsystem_name = NULL; *************** *** 1346,1350 **** ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes)); ! if (!PyArg_ParseTuple(args, "Oi:GetPolicyHandle", &obsystem_name, // @pyparm string/<o PyUnicode>|obsystem_name||System name, local system assumed if not specified &access_mask)) // @pyparm int|access_mask||Bitmask of requested access types --- 1416,1420 ---- ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes)); ! if (!PyArg_ParseTuple(args, "Oi:LsaOpenPolicy", &obsystem_name, // @pyparm string/<o PyUnicode>|obsystem_name||System name, local system assumed if not specified &access_mask)) // @pyparm int|access_mask||Bitmask of requested access types *************** *** 1355,1362 **** ntsResult = LsaOpenPolicy(&system_name, &ObjectAttributes, access_mask, &lsahPolicyHandle); if (ntsResult != STATUS_SUCCESS){ ! PyWin_SetAPIError("GetPolicyHandle",LsaNtStatusToWinError(ntsResult)); goto done; } ! ret = PyWinObject_FromHANDLE(lsahPolicyHandle); done: PyWinObject_FreeTCHAR(system_name.Buffer); --- 1425,1432 ---- ntsResult = LsaOpenPolicy(&system_name, &ObjectAttributes, access_mask, &lsahPolicyHandle); if (ntsResult != STATUS_SUCCESS){ ! PyWin_SetAPIError("LsaOpenPolicy",LsaNtStatusToWinError(ntsResult)); goto done; } ! ret = PyWinObject_FromLSA_HANDLE(lsahPolicyHandle); done: PyWinObject_FreeTCHAR(system_name.Buffer); *************** *** 1370,1385 **** static PyObject *PyLsaClose(PyObject *self, PyObject *args) { ! PyObject *obhandle; ! LSA_HANDLE lsah; ! NTSTATUS err; ! if (!PyArg_ParseTuple(args, "O:LsaClose", &obhandle)) return NULL; ! if (!PyWinObject_AsHANDLE(obhandle, &lsah)) ! return NULL; ! err=LsaClose(lsah); ! if (err != STATUS_SUCCESS){ ! PyWin_SetAPIError("LsaClose",LsaNtStatusToWinError(err)); return NULL; - } Py_INCREF(Py_None); return Py_None; --- 1440,1449 ---- static PyObject *PyLsaClose(PyObject *self, PyObject *args) { ! PyObject *obHandle; ! if (!PyArg_ParseTuple(args, "O:LsaClose", &obHandle)) return NULL; ! ! if (!PyWinObject_CloseLSA_HANDLE(obHandle)) return NULL; Py_INCREF(Py_None); return Py_None; *************** *** 1883,1886 **** --- 1947,2135 ---- %} + // @pyswig |LsaStorePrivateData|Stores encrypted unicode data under specified Lsa registry key. Returns None on success + %native(LsaStorePrivateData) PyLsaStorePrivateData; + %{ + static PyObject *PyLsaStorePrivateData(PyObject *self, PyObject *args) + { + // @pyparm <o PyHANDLE>||Policy handle + // @pyparm string|KeyName||Registry key in which to store data + // @pyparm int|PrivateData||Unicode string to be encrypted and stored + PyObject *obpolicyhandle=NULL, *obkeyname=NULL, *obprivatedata=NULL; + PyObject * ret=NULL; + LSA_HANDLE policyhandle; + LSA_UNICODE_STRING keyname, privatedata; + keyname.Buffer=NULL; + privatedata.Buffer=NULL; + NTSTATUS err = NULL; + if (!PyArg_ParseTuple(args, "OOO:LsaStorePrivateData", &obpolicyhandle, &obkeyname, &obprivatedata)) + return NULL; + if (!PyWinObject_AsHANDLE(obpolicyhandle, &policyhandle)) + return NULL; + if (!PyWinObject_AsLSA_UNICODE_STRING(obkeyname, &keyname, FALSE)) + goto done; + // passing NULL deletes the data stored under specified key + // use Py_None since empty string is considered valid data + if (obprivatedata==Py_None) + err = LsaStorePrivateData(policyhandle, &keyname, NULL); + else{ + if (!PyWinObject_AsLSA_UNICODE_STRING(obprivatedata, &privatedata, FALSE)) + goto done; + err = LsaStorePrivateData(policyhandle, &keyname, &privatedata); + } + if (err == STATUS_SUCCESS) + ret=Py_None; + else + PyWin_SetAPIError("LsaStorePrivateData",LsaNtStatusToWinError(err)); + + done: + if (keyname.Buffer != NULL) + PyWinObject_FreeWCHAR(keyname.Buffer); + if (privatedata.Buffer != NULL) + PyWinObject_FreeWCHAR(privatedata.Buffer); + Py_XINCREF(ret); + return ret; + } + %} + + // @pyswig <o PyUnicode>|LsaRetrievePrivateData|Retreives encrypted unicode data from Lsa registry key. + %native(LsaRetrievePrivateData) PyLsaRetrievePrivateData; + %{ + static PyObject *PyLsaRetrievePrivateData(PyObject *self, PyObject *args) + { + // @pyparm <o PyHANDLE>||Policy handle + // @pyparm string|KeyName||Registry key to read + PyObject *obpolicyhandle=NULL, *obkeyname=NULL, *obprivatedata=NULL; + PyObject * ret=NULL; + LSA_HANDLE policyhandle; + LSA_UNICODE_STRING keyname; + keyname.Buffer=NULL; + PLSA_UNICODE_STRING privatedata = NULL; + NTSTATUS err = NULL; + if (!PyArg_ParseTuple(args, "OO:LsaRetrievePrivateData", &obpolicyhandle, &obkeyname)) + return NULL; + if (!PyWinObject_AsHANDLE(obpolicyhandle, &policyhandle)) + return NULL; + if (!PyWinObject_AsLSA_UNICODE_STRING(obkeyname, &keyname, FALSE)) + goto done; + + err = LsaRetrievePrivateData(policyhandle, &keyname, &privatedata); + if (err == STATUS_SUCCESS) + ret=PyWinObject_FromLSA_UNICODE_STRING(*privatedata); + else + PyWin_SetAPIError("LsaRetrievePrivateData",LsaNtStatusToWinError(err)); + done: + if (keyname.Buffer != NULL) + PyWinObject_FreeWCHAR(keyname.Buffer); + if (privatedata != NULL) + LsaFreeMemory(privatedata); + return ret; + } + %} + + // @pyswig object|LsaRegisterPolicyChangeNotification|Register an event handle to receive policy change events + %native(LsaRegisterPolicyChangeNotification) PyLsaRegisterPolicyChangeNotification; + %{ + static PyObject *PyLsaRegisterPolicyChangeNotification(PyObject *self, PyObject *args) + { + if (lsarpcn==NULL) + return PyErr_Format(PyExc_NotImplementedError,"LsaRegisterPolicyChangeNotification not supported by this version of Windows"); + PyObject *obHandle=NULL; + PyObject *ret=NULL; + HANDLE hevent; + POLICY_NOTIFICATION_INFORMATION_CLASS info_class; + NTSTATUS err; + if (!PyArg_ParseTuple(args, "lO:LsaRegisterPolicyChangeNotification", + (long *)&info_class, // @pyparm int|InformationClass||One of POLICY_NOTIFICATION_INFORMATION_CLASS contants + &obHandle)) // @pyparm <o PyHANDLE>|NotificationEventHandle||Event handle to receives notification + return NULL; + if (!PyWinObject_AsHANDLE(obHandle, &hevent, FALSE)) + return NULL; + err=(*lsarpcn)(info_class,hevent); + if (err==STATUS_SUCCESS) + ret=Py_None; + else + PyWin_SetAPIError("LsaRegisterPolicyChangeNotification",LsaNtStatusToWinError(err)); + Py_XINCREF(ret); + return ret; + } + %} + + // @pyswig object|LsaUnregisterPolicyChangeNotification|Stop receiving policy change notification + %native(LsaUnregisterPolicyChangeNotification) PyLsaUnregisterPolicyChangeNotification; + %{ + static PyObject *PyLsaUnregisterPolicyChangeNotification(PyObject *self, PyObject *args) + { + if (lsaupcn==NULL) + return PyErr_Format(PyExc_NotImplementedError,"LsaUnregisterPolicyChangeNotification not supported by this version of Windows"); + PyObject *obHandle; + PyObject *ret=NULL; + HANDLE hevent; + POLICY_NOTIFICATION_INFORMATION_CLASS info_class; + NTSTATUS err; + if (!PyArg_ParseTuple(args, "lO:LsaUnregisterPolicyChangeNotification", + (long *)&info_class, // @pyparm int|InformationClass||POLICY_NOTIFICATION_INFORMATION_CLASS constant + &obHandle)) // @pyparm <o PyHANDLE>|NotificationEventHandle||Event handle previously registered to receive policy change events + return NULL; + if (!PyWinObject_AsHANDLE(obHandle, &hevent, FALSE)) + return NULL; + err=(*lsaupcn)(info_class,hevent); + if (err==STATUS_SUCCESS) + ret=Py_None; + else + PyWin_SetAPIError("LsaUnregisterPolicyChangeNotification",LsaNtStatusToWinError(err)); + Py_XINCREF(ret); + return ret; + } + %} + + // @pyswig object|CryptEnumProviders|List cryptography providers + %native(CryptEnumProviders) PyCryptEnumProviders; + %{ + static PyObject *PyCryptEnumProviders(PyObject *self, PyObject *args) + { + if (!PyArg_ParseTuple(args, ":CryptEnumProviders")) + return NULL; + DWORD dwFlags=0, dwIndex=0, dwReserved=NULL, dwProvType=0, cbProvName=0; + WCHAR *pszProvName=NULL; + PyObject *ret=PyList_New(0); + PyObject *ret_item=NULL; + BOOL succeeded = TRUE; + DWORD err = 0; + do{ + cbProvName=0; + pszProvName=NULL; + succeeded = CryptEnumProviders(dwIndex, NULL, dwFlags, &dwProvType, NULL, &cbProvName); + if (!succeeded) + break; + // test breaking out of loop if out of memory + // if (dwIndex==3) + // cbProvName=0x77777777; + pszProvName = (WCHAR *)malloc(cbProvName); + if (pszProvName==NULL){ + Py_DECREF(ret); + PyErr_SetString(PyExc_MemoryError, "CryptEnumProviders: SOM"); + return NULL; + } + succeeded = CryptEnumProviders(dwIndex, NULL, dwFlags, &dwProvType, pszProvName, &cbProvName); + if (!succeeded){ + free(pszProvName); + break; + } + ret_item = Py_BuildValue("ul",pszProvName, dwProvType); + PyList_Append(ret, ret_item); + Py_DECREF(ret_item); + free(pszProvName); + dwIndex++; + } + while (succeeded); + err=GetLastError(); + if (err != ERROR_NO_MORE_ITEMS){ + Py_DECREF(ret); + ret=NULL; + PyWin_SetAPIError("CryptEnumProviders",err); + } + return ret; + } + %} #define TOKEN_ADJUST_DEFAULT TOKEN_ADJUST_DEFAULT // Required to change the default ACL, primary group, or owner of an access token. *************** *** 2074,2081 **** --- 2323,2334 ---- #ifdef PolicyServerEnabled #define PolicyServerEnabled PolicyServerEnabled + #else + #define PolicyServerEnabled 2 #endif #ifdef PolicyServerDisabled #define PolicyServerDisabled PolicyServerDisabled + #else + #define PolicyServerDisabled 3 #endif |