Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
3.9 KiB

  1. /********************************************************************/
  2. /** Copyright(c) 1995 Microsoft Corporation. **/
  3. /********************************************************************/
  4. //***
  5. //
  6. // Filename: misc.c
  7. //
  8. // Description: Contains miscillaneous functions and routines
  9. //
  10. // History: Feb 11,1998 NarenG Created original version.
  11. //
  12. #define UNICODE
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include <rtutils.h>
  18. #include <lmcons.h>
  19. #include <rasauth.h>
  20. #define INCL_MISC
  21. #include "ppputil.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. //**
  25. //
  26. // Call: ExtractUsernameAndDomain
  27. //
  28. // Returns: NO_ERROR - Success
  29. // Non-zero returns - Failure
  30. //
  31. // Description:
  32. //
  33. DWORD
  34. ExtractUsernameAndDomain(
  35. IN LPSTR szIdentity,
  36. OUT LPSTR szUserName,
  37. OUT LPSTR szDomainName OPTIONAL
  38. )
  39. {
  40. WCHAR * pwchIdentity = NULL;
  41. WCHAR * pwchColon = NULL;
  42. WCHAR * pwchBackSlash = NULL;
  43. WCHAR * wszIdentity = NULL;
  44. DWORD dwLen, dwSize;
  45. DWORD dwErr = NO_ERROR;
  46. *szUserName = (CHAR)NULL;
  47. if ( szDomainName != NULL )
  48. {
  49. *szDomainName = (CHAR)NULL;
  50. }
  51. //
  52. // First, allocate a buffer to hold the unicode version of the
  53. // identity string
  54. //
  55. dwLen = strlen(szIdentity);
  56. if ( dwLen == 0 )
  57. {
  58. dwErr = ERROR_BAD_USERNAME;
  59. goto LDone;
  60. }
  61. dwSize = (dwLen + 1) * sizeof(WCHAR);
  62. //
  63. // Convert identity to UNICODE string for user name so that
  64. // a search for '\\' doesn't accidentally succeed. (bug 152088)
  65. //
  66. wszIdentity = LocalAlloc ( LPTR, dwSize );
  67. if ( wszIdentity == NULL )
  68. {
  69. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  70. goto LDone;
  71. }
  72. if ( 0 == MultiByteToWideChar(
  73. CP_ACP,
  74. 0,
  75. szIdentity,
  76. -1,
  77. wszIdentity,
  78. dwLen + 1 ) )
  79. {
  80. dwErr = GetLastError();
  81. goto LDone;
  82. }
  83. pwchIdentity = wszIdentity;
  84. //
  85. // Parse out username and domain from the Name (domain\username or
  86. // username format).
  87. //
  88. if ( ( pwchBackSlash = wcschr( wszIdentity, L'\\' ) ) != NULL )
  89. {
  90. //
  91. // Extract the domain.
  92. //
  93. DWORD cbDomain;
  94. //
  95. // Get the domain the user wants to logon to, if one was specified,
  96. // and convert to UNICODE.
  97. //
  98. cbDomain = (DWORD)(pwchBackSlash - pwchIdentity);
  99. if ( cbDomain > DNLEN )
  100. {
  101. dwErr = ERROR_BAD_USERNAME;
  102. goto LDone;
  103. }
  104. if ( szDomainName != NULL )
  105. {
  106. dwLen = WideCharToMultiByte(
  107. CP_ACP,
  108. 0,
  109. pwchIdentity,
  110. cbDomain,
  111. szDomainName,
  112. cbDomain,
  113. NULL,
  114. NULL );
  115. if ( dwLen > 0 )
  116. {
  117. szDomainName[ dwLen ] = 0;
  118. }
  119. else
  120. {
  121. szDomainName[ 0 ] = 0;
  122. }
  123. }
  124. pwchIdentity = pwchBackSlash + 1;
  125. }
  126. else
  127. {
  128. //
  129. // No domain name
  130. //
  131. if ( szDomainName != NULL )
  132. {
  133. szDomainName[ 0 ] = '\0';
  134. }
  135. }
  136. dwLen = wcslen( pwchIdentity );
  137. if ( dwLen > UNLEN )
  138. {
  139. dwErr = ERROR_BAD_USERNAME;
  140. goto LDone;
  141. }
  142. if ( 0 == WideCharToMultiByte(
  143. CP_ACP,
  144. 0,
  145. pwchIdentity,
  146. -1,
  147. szUserName,
  148. UNLEN + 1,
  149. NULL,
  150. NULL ) )
  151. {
  152. dwErr = GetLastError();
  153. goto LDone;
  154. }
  155. LDone:
  156. LocalFree( wszIdentity );
  157. return( dwErr );
  158. }