Leaked source code of windows server 2003
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.

264 lines
6.0 KiB

  1. /*++
  2. MAPUSER.CXX
  3. Copyright (C) 1999 Microsoft Corporation, all rights reserved.
  4. DESCRIPTION: code for MapUser()
  5. Created, May 21, 1999 by DavidCHR.
  6. --*/
  7. #include "everything.hxx"
  8. extern "C" {
  9. #include <malloc.h> // alloca
  10. #include "..\keytab2\keytab\ldlib\delegtools.h"
  11. }
  12. static CHAR AltSecId[] = "AltSecurityIdentities";
  13. static CHAR AltSecPrefix[] = "KERBEROS:";
  14. static CHAR PreQuery[] = "(objectClass=user)"; /* For performance
  15. reasons, we should
  16. query an indexed type */
  17. NTSTATUS
  18. MapUserInDirectory( IN LPWSTR Principal,
  19. IN OPTIONAL LPWSTR Account ) {
  20. LPSTR Attributes[] = { NULL }; // request no attributes
  21. PCHAR PrincValues[] = { NULL, NULL };
  22. LDAPModA TheMod = { LDAP_MOD_DELETE,
  23. AltSecId,
  24. PrincValues };
  25. PLDAPModA Mods[] = { &TheMod, NULL };
  26. CHAR SearchBuffer [ UNLEN + 100 ]; /* The most we could have to
  27. search for is UNLEN (for either
  28. the principalname or the
  29. accountname) + 100 for the
  30. semantics of the query */
  31. NTSTATUS ret = STATUS_INTERNAL_ERROR;
  32. LPSTR ObjectDn;
  33. ULONG lderr;
  34. if ( ( lstrcmpW( Principal, L"*" ) == 0 ) ||
  35. ( Account && ( lstrcmpW( Account, L"*" ) == 0 ) ) ) {
  36. printf( "Wildcard account mappings are not supported"
  37. " at the domain level.\n" );
  38. return STATUS_NOT_SUPPORTED;
  39. }
  40. if ( ConnectedToDsa() ) {
  41. if ( Account ) { // changing the attribute -- search for the account
  42. wsprintfA( SearchBuffer,
  43. "(& %hs (samAccountName=%ws))",
  44. PreQuery,
  45. Account );
  46. } else { // deleting the attribute -- search for the attr
  47. wsprintfA( SearchBuffer,
  48. "(& %hs (%hs=%hs%ws))",
  49. PreQuery,
  50. AltSecId,
  51. AltSecPrefix,
  52. Principal );
  53. }
  54. if ( LdapSearchForUniqueDnA( GlobalLdap,
  55. SearchBuffer,
  56. Attributes,
  57. &ObjectDn,
  58. NULL ) ) {
  59. PrincValues[ 0 ] = (PCHAR)malloc( lstrlenW( Principal ) + 30 );
  60. if ( !PrincValues[ 0 ] ) {
  61. return STATUS_NO_MEMORY; /* NOTE: 73954: This leaks, but the
  62. app terminates immediately afterwards,
  63. so we don't actually care. */
  64. }
  65. wsprintfA( PrincValues[ 0 ],
  66. "%hs%ws",
  67. AltSecPrefix,
  68. Principal );
  69. if ( Account ) {
  70. TheMod.mod_op = LDAP_MOD_ADD;
  71. } else {
  72. TheMod.mod_op = LDAP_MOD_DELETE;
  73. }
  74. lderr = ldap_modify_sA( GlobalLdap,
  75. ObjectDn,
  76. Mods );
  77. // special-case output here:
  78. switch( lderr ) {
  79. case LDAP_SUCCESS:
  80. printf( "Mapping %hs successfully.\n",
  81. Account ? "created" : "deleted" );
  82. ret = STATUS_SUCCESS;
  83. break;
  84. default:
  85. printf( "Failed to %hs %hs on %hs; error 0x%x.\n",
  86. Account ? "set" : "delete",
  87. AltSecId,
  88. ObjectDn,
  89. lderr );
  90. ret = STATUS_UNSUCCESSFUL;
  91. break;
  92. }
  93. free( ObjectDn );
  94. free( PrincValues[0] );
  95. } else {
  96. printf( "Could not locate the account mapping in the directory.\n" );
  97. }
  98. }
  99. return ret;
  100. }
  101. NTSTATUS
  102. MapUserInRegistry( IN LPWSTR Principal,
  103. IN OPTIONAL LPWSTR Account ) {
  104. DWORD RegErr;
  105. HKEY KerbHandle = NULL;
  106. HKEY UserListHandle = NULL;
  107. DWORD Disposition;
  108. RegErr = OpenKerberosKey(&KerbHandle);
  109. if (RegErr)
  110. {
  111. goto Cleanup;
  112. }
  113. RegErr = RegCreateKeyEx(
  114. KerbHandle,
  115. L"UserList",
  116. 0,
  117. NULL,
  118. 0, // no options
  119. KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
  120. NULL,
  121. &UserListHandle,
  122. &Disposition
  123. );
  124. if (RegErr)
  125. {
  126. printf("Failed to create UserList key: %d\n",RegErr);
  127. goto Cleanup;
  128. }
  129. if ( Account && Account[0] ) {
  130. RegErr = RegSetValueEx( UserListHandle,
  131. Principal,
  132. 0,
  133. REG_SZ,
  134. (PBYTE) Account,
  135. (wcslen(Account) + 1) * sizeof(WCHAR)
  136. );
  137. if (RegErr)
  138. {
  139. printf("Failed to set name mapping value: %d\n",RegErr);
  140. goto Cleanup;
  141. }
  142. } else {
  143. /* if no second parameter was supplied,
  144. delete the mapping. */
  145. RegErr = RegDeleteValue( UserListHandle,
  146. Principal );
  147. switch( RegErr ) {
  148. case ERROR_PATH_NOT_FOUND:
  149. case ERROR_FILE_NOT_FOUND:
  150. RegErr = ERROR_SUCCESS;
  151. // fallthrough to success case
  152. case ERROR_SUCCESS:
  153. break;
  154. default:
  155. printf( "Failed to delete mapping for %ws: error 0x%x.\n",
  156. Principal,
  157. RegErr );
  158. goto Cleanup;
  159. }
  160. }
  161. Cleanup:
  162. if (KerbHandle)
  163. {
  164. RegCloseKey(KerbHandle);
  165. }
  166. if (UserListHandle)
  167. {
  168. RegCloseKey(UserListHandle);
  169. }
  170. if (RegErr)
  171. {
  172. return(STATUS_UNSUCCESSFUL);
  173. }
  174. return(STATUS_SUCCESS);
  175. }
  176. NTSTATUS
  177. MapUser( IN LPWSTR * Parameters ) {
  178. //
  179. // Mapuser needs at least one none-empty argument
  180. //
  181. if (!Parameters[ 0 ] || !*Parameters[ 0 ])
  182. {
  183. return STATUS_INVALID_PARAMETER;
  184. }
  185. return ( GlobalDomainSetting ?
  186. MapUserInDirectory :
  187. MapUserInRegistry )( Parameters[ 0 ],
  188. Parameters[ 1 ] );
  189. }