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.

290 lines
7.1 KiB

  1. #include "main.h"
  2. #define PWLEN 256
  3. BOOL
  4. GetPassword(
  5. PWSTR szBuffer,
  6. DWORD dwLength,
  7. DWORD *pdwLengthReturn
  8. );
  9. extern "C" int __cdecl wmain(
  10. IN int argc,
  11. IN PWSTR argv[]
  12. )
  13. {
  14. DWORD WinError = 0;
  15. PWSTR szTree = NULL;
  16. PWSTR szUser = NULL;
  17. PWSTR szContext = NULL;
  18. PWSTR szPwd = NULL;
  19. WCHAR szBuffer[PWLEN+1];
  20. DWORD dwLength;
  21. InitMem();
  22. //
  23. // either with 4 parameters or 2
  24. //
  25. if((argc != 5) && (argc != 3)) {
  26. SelectivePrint(MSG_HELP);
  27. BAIL();
  28. }
  29. if (argc == 5) {
  30. if (!(argv[1] && argv[2] && argv[3] && argv[4])) {
  31. SelectivePrint(MSG_HELP);
  32. BAIL();
  33. }
  34. }
  35. else {
  36. ASSERT(argc == 3);
  37. if (!(argv[1] && argv[2])) {
  38. SelectivePrint(MSG_HELP);
  39. BAIL();
  40. }
  41. }
  42. szTree = argv[2];
  43. if (argc == 5) {
  44. szUser = argv[3];
  45. szPwd = argv[4];
  46. if ((wcscmp(szPwd,L"*") == 0)) {
  47. SelectivePrint(MSG_GETPASSWORD,
  48. szTree);
  49. if (GetPassword(szBuffer,PWLEN+1,&dwLength)) {
  50. szPwd = szBuffer;
  51. }
  52. else {
  53. SelectivePrint(MSG_PASSWORDTOLONG);
  54. BAIL();
  55. }
  56. }
  57. while (*szUser) {
  58. if (*szUser == '.') {
  59. *szUser = NULL;
  60. szContext = szUser+1;
  61. break;
  62. }
  63. szUser++;
  64. }
  65. szUser = argv[3];
  66. }
  67. if (_wcsicmp(argv[1],g_szExtend) == 0) {
  68. WinError = Client32ExtendSchema(szTree,
  69. szContext,
  70. szUser,
  71. szPwd);
  72. if (WinError == ERROR_MOD_NOT_FOUND) {
  73. WinError = NWAPIExtendSchema(szTree,
  74. szContext,
  75. szUser,
  76. szPwd);
  77. if (WinError) {
  78. if (WinError == 1) {
  79. WinError = 0;
  80. SelectivePrint(MSG_EXTENDED_ALREADY, szTree);
  81. }
  82. BAIL();
  83. }
  84. else {
  85. SelectivePrint(MSG_EXTEND_SUCCESS,szTree);
  86. }
  87. }
  88. else if (WinError) {
  89. if (WinError == 1) {
  90. WinError = 0;
  91. SelectivePrint(MSG_EXTENDED_ALREADY, szTree);
  92. }
  93. BAIL();
  94. }
  95. else {
  96. SelectivePrint(MSG_EXTEND_SUCCESS,szTree);
  97. }
  98. }
  99. else if (_wcsicmp(argv[1],g_szCheck) == 0) {
  100. BOOL fExtended;
  101. WinError = Client32CheckSchemaExtension(szTree,
  102. szContext,
  103. szUser,
  104. szPwd,
  105. &fExtended);
  106. if (WinError == ERROR_MOD_NOT_FOUND) {
  107. WinError = NWAPICheckSchemaExtension(szTree,
  108. szContext,
  109. szUser,
  110. szPwd,
  111. &fExtended);
  112. if (WinError) {
  113. BAIL();
  114. }
  115. }
  116. else if (WinError) {
  117. BAIL();
  118. }
  119. if (fExtended) {
  120. WinError = 1;
  121. SelectivePrint(MSG_EXTENDED,szTree);
  122. }
  123. else {
  124. WinError = 0;
  125. SelectivePrint(MSG_NOT_EXTENDED,szTree);
  126. }
  127. }
  128. else {
  129. SelectivePrint(MSG_HELP);
  130. BAIL();
  131. }
  132. error:
  133. if (WinError && (WinError != 1)) {
  134. SelectivePrint(MSG_ERROR,WinError);
  135. SelectivePrintWin32(WinError);
  136. }
  137. return WinError;
  138. }
  139. void SelectivePrint(DWORD messageID, ...)
  140. {
  141. static BOOLEAN bTriedOpen = FALSE;
  142. PWSTR pszMessageBuffer = NULL;
  143. DWORD dwSize;
  144. va_list ap;
  145. DWORD dwLen;
  146. va_start(ap, messageID);
  147. dwLen = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  148. NULL,
  149. messageID,
  150. 0,
  151. (PWSTR)&pszMessageBuffer,
  152. 4095,
  153. &ap);
  154. if (dwLen == 0)
  155. {
  156. DWORD WinError = GetLastError();
  157. ERR(("Error formatting message: %d\n", WinError));
  158. BAIL();
  159. }
  160. dwSize = wcslen(pszMessageBuffer);
  161. if (pszMessageBuffer[dwSize-2] == '\r') {
  162. pszMessageBuffer[dwSize-2] = '\n';
  163. pszMessageBuffer[dwSize-1] = '\0';
  164. }
  165. fwprintf(stdout,pszMessageBuffer);
  166. error:
  167. va_end(ap);
  168. if (pszMessageBuffer) {
  169. LocalFree(pszMessageBuffer);
  170. }
  171. return;
  172. }
  173. void SelectivePrintWin32(DWORD dwError)
  174. {
  175. DWORD dwLen;
  176. WCHAR szMessage[256];
  177. dwLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  178. NULL,
  179. dwError,
  180. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  181. szMessage,
  182. 256,
  183. NULL);
  184. if (dwLen == 0)
  185. {
  186. DWORD WinError = GetLastError();
  187. ERR(("Error formatting message: %d\n", WinError));
  188. BAIL();
  189. }
  190. fwprintf(stdout,szMessage);
  191. error:
  192. return;
  193. }
  194. #define CR 0xD
  195. #define BACKSPACE 0x8
  196. #define NULLC '\0'
  197. #define NEWLINE '\n'
  198. BOOL
  199. GetPassword(
  200. PWSTR szBuffer,
  201. DWORD dwLength,
  202. DWORD *pdwLengthReturn
  203. )
  204. {
  205. WCHAR ch;
  206. PWSTR pszBufCur = szBuffer;
  207. DWORD c;
  208. int err;
  209. DWORD mode;
  210. //
  211. // make space for NULL terminator
  212. //
  213. dwLength -= 1;
  214. *pdwLengthReturn = 0;
  215. GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
  216. &mode);
  217. SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
  218. (~(ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT)) & mode);
  219. while (TRUE) {
  220. err = ReadConsole(GetStdHandle(STD_INPUT_HANDLE),
  221. &ch,
  222. 1,
  223. &c,
  224. 0);
  225. if (!err || c != 1)
  226. ch = 0xffff;
  227. if ((ch == CR) || (ch == 0xffff)) // end of line
  228. break;
  229. if (ch == BACKSPACE) { // back up one or two
  230. //
  231. // IF pszBufCur == buf then the next two lines are a no op.
  232. // Because the user has basically backspaced back to the start
  233. //
  234. if (pszBufCur != szBuffer) {
  235. pszBufCur--;
  236. (*pdwLengthReturn)--;
  237. }
  238. }
  239. else {
  240. *pszBufCur = ch;
  241. if (*pdwLengthReturn < dwLength)
  242. pszBufCur++ ; // don't overflow buf
  243. (*pdwLengthReturn)++; // always increment pdwLengthReturn
  244. }
  245. }
  246. SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode);
  247. //
  248. // NULL terminate the string
  249. //
  250. *pszBufCur = NULLC;
  251. putchar(NEWLINE);
  252. return((*pdwLengthReturn <= dwLength) ? TRUE : FALSE);
  253. }