WindowsAuthentication.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. static struct auth alwaysauth;
  14. static char sidbuf[4096];
  15. static PSID psid = (PSID)sidbuf;
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. static int windowsfunc(struct clientparam *param){
  20. char *dom;
  21. HANDLE h;
  22. DWORD dw, sidlen, i;
  23. char tokenbuf[4096];
  24. PTOKEN_GROUPS ptg = (PTOKEN_GROUPS)tokenbuf;
  25. if(!param->username || !param->password || param->pwtype != 0) return 4;
  26. dom = strchr((char *)param->username, '\\');
  27. if(dom)*dom++=0;
  28. if(!LogonUser( dom?dom:(char *)param->username,
  29. dom?(char *)param->username:NULL,
  30. (char *)param->password,
  31. LOGON32_LOGON_NETWORK,
  32. LOGON32_PROVIDER_DEFAULT,
  33. &h))return 5;
  34. if(dom)*(dom-1)='\\';
  35. if(!GetTokenInformation(h, TokenGroups, ptg, sizeof(tokenbuf), &dw)) return 6;
  36. CloseHandle(h);
  37. sidlen = GetLengthSid(psid);
  38. for(i=0; i < ptg->GroupCount; i++){
  39. if(GetLengthSid(ptg->Groups[i].Sid)==sidlen){
  40. if(!memcmp((void *)ptg->Groups[i].Sid, (void *)psid, sidlen)) {
  41. setlocale(LC_CTYPE, ".ACP");
  42. _strlwr((char *)param->username);
  43. return 0;
  44. }
  45. }
  46. }
  47. return 7;
  48. }
  49. #ifdef WATCOM
  50. #pragma aux WindowsAuthentication "*" parm caller [ ] value struct float struct routine [eax] modify [eax ecx edx]
  51. #undef PLUGINCALL
  52. #define PLUGINCALL
  53. #endif
  54. PLUGINAPI int PLUGINCALL 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.preauthorize = pluginlink->checkpreACL;
  67. alwaysauth.authenticate = windowsfunc;
  68. alwaysauth.authorize = pluginlink->checkACL;
  69. alwaysauth.desc = "windows";
  70. alwaysauth.next = pluginlink->authfuncs->next;
  71. pluginlink->authfuncs->next = &alwaysauth;
  72. loaded = 1;
  73. }
  74. return 0;
  75. }
  76. #ifdef __cplusplus
  77. }
  78. #endif