1
0

winnpc.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Windows support module which deals with being a named-pipe client.
  3. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include "tree234.h"
  7. #include "putty.h"
  8. #include "network.h"
  9. #include "proxy.h"
  10. #include "ssh.h"
  11. #if !defined NO_SECURITY
  12. #include "winsecur.h"
  13. HANDLE connect_to_named_pipe(const char *pipename, char **err)
  14. {
  15. HANDLE pipehandle;
  16. PSID usersid, pipeowner;
  17. PSECURITY_DESCRIPTOR psd;
  18. assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
  19. assert(strchr(pipename + 9, '\\') == NULL);
  20. while (1) {
  21. pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE,
  22. 0, NULL, OPEN_EXISTING,
  23. FILE_FLAG_OVERLAPPED, NULL);
  24. if (pipehandle != INVALID_HANDLE_VALUE)
  25. break;
  26. if (GetLastError() != ERROR_PIPE_BUSY) {
  27. *err = dupprintf(
  28. "Unable to open named pipe '%s': %s",
  29. pipename, win_strerror(GetLastError()));
  30. return INVALID_HANDLE_VALUE;
  31. }
  32. /*
  33. * If we got ERROR_PIPE_BUSY, wait for the server to
  34. * create a new pipe instance. (Since the server is
  35. * expected to be winnps.c, which will do that immediately
  36. * after a previous connection is accepted, that shouldn't
  37. * take excessively long.)
  38. */
  39. if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) {
  40. *err = dupprintf(
  41. "Error waiting for named pipe '%s': %s",
  42. pipename, win_strerror(GetLastError()));
  43. return INVALID_HANDLE_VALUE;
  44. }
  45. }
  46. if ((usersid = get_user_sid()) == NULL) {
  47. CloseHandle(pipehandle);
  48. *err = dupprintf(
  49. "Unable to get user SID: %s", win_strerror(GetLastError()));
  50. return INVALID_HANDLE_VALUE;
  51. }
  52. if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
  53. OWNER_SECURITY_INFORMATION,
  54. &pipeowner, NULL, NULL, NULL,
  55. &psd) != ERROR_SUCCESS) {
  56. CloseHandle(pipehandle);
  57. *err = dupprintf(
  58. "Unable to get named pipe security information: %s",
  59. win_strerror(GetLastError()));
  60. return INVALID_HANDLE_VALUE;
  61. }
  62. if (!EqualSid(pipeowner, usersid)) {
  63. CloseHandle(pipehandle);
  64. LocalFree(psd);
  65. *err = dupprintf(
  66. "Owner of named pipe '%s' is not us", pipename);
  67. return INVALID_HANDLE_VALUE;
  68. }
  69. LocalFree(psd);
  70. return pipehandle;
  71. }
  72. Socket *new_named_pipe_client(const char *pipename, Plug *plug)
  73. {
  74. char *err = NULL;
  75. HANDLE pipehandle = connect_to_named_pipe(pipename, &err);
  76. if (pipehandle == INVALID_HANDLE_VALUE)
  77. return new_error_socket_consume_string(plug, err);
  78. else
  79. return make_handle_socket(pipehandle, pipehandle, NULL, plug, true);
  80. }
  81. #endif /* !defined NO_SECURITY */