WindowsAuthentication.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2007-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. */
  6. #include "../../structures.h"
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <locale.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. __declspec(dllexport) int WindowsAuthentication(struct pluginlink * pluginlink,
  14. int argc, char** argv);
  15. static struct auth alwaysauth;
  16. static char sidbuf[4096];
  17. static PSID psid = (PSID)sidbuf;
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. static int windowsfunc(struct clientparam *param){
  22. char *dom;
  23. HANDLE h;
  24. DWORD dw, sidlen, i;
  25. char tokenbuf[4096];
  26. PTOKEN_GROUPS ptg = (PTOKEN_GROUPS)tokenbuf;
  27. if(!param->username || !param->password || param->pwtype != 0) return 4;
  28. dom = strchr((char *)param->username, '\\');
  29. if(dom)*dom++=0;
  30. if(!LogonUser( dom?dom:(char *)param->username,
  31. dom?(char *)param->username:NULL,
  32. param->password,
  33. LOGON32_LOGON_NETWORK,
  34. LOGON32_PROVIDER_DEFAULT,
  35. &h))return 5;
  36. if(dom)*(dom-1)='\\';
  37. if(!GetTokenInformation(h, TokenGroups, ptg, sizeof(tokenbuf), &dw)) return 6;
  38. CloseHandle(h);
  39. sidlen = GetLengthSid(psid);
  40. for(i=0; i < ptg->GroupCount; i++){
  41. if(GetLengthSid(ptg->Groups[i].Sid)==sidlen){
  42. if(!memcmp((void *)ptg->Groups[i].Sid, (void *)psid, sidlen)) {
  43. setlocale(LC_CTYPE, ".ACP");
  44. _strlwr(param->username);
  45. return 0;
  46. }
  47. }
  48. }
  49. return 7;
  50. }
  51. #ifdef __cplusplus
  52. }
  53. #endif
  54. int WindowsAuthentication(struct pluginlink * pluginlink, int argc, char** argv){
  55. char tmpbuf[4096];
  56. DWORD dlen, sidlen;
  57. SID_NAME_USE snu;
  58. static int loaded = 0;
  59. if(argc != 2) return 11;
  60. dlen = sizeof(tmpbuf)/sizeof(TCHAR);
  61. sidlen = sizeof(sidbuf);
  62. if(!LookupAccountName(NULL, argv[1], psid, &sidlen,
  63. (LPTSTR) tmpbuf, &dlen, &snu)) return 100000 + (int)GetLastError();
  64. if(snu != SidTypeGroup && snu != SidTypeAlias && snu != SidTypeWellKnownGroup) return 12;
  65. if(!loaded){
  66. alwaysauth.authenticate = windowsfunc;
  67. alwaysauth.authorize = pluginlink->checkACL;
  68. alwaysauth.desc = "windows";
  69. alwaysauth.next = pluginlink->authfuncs->next;
  70. pluginlink->authfuncs->next = &alwaysauth;
  71. loaded = 1;
  72. }
  73. return 0;
  74. }