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.

164 lines
3.9 KiB

  1. /**************************************************************************\
  2. * LPLow.C *
  3. *------------------------------------------------------------------------*
  4. * *
  5. * PPR Support for low level network and path handling. *
  6. * *
  7. \**************************************************************************/
  8. #include <nt.h>
  9. #include <ntrtl.h>
  10. #include <nturtl.h>
  11. #include <windef.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #define INCL_NETUSE
  15. #define INCL_NETWKSTA
  16. #define INCL_NETERRORS
  17. #include <windows.h>
  18. #include "lpr.h"
  19. extern BOOL fVerify; /* From LPR.C */
  20. BOOL fRedir = FALSE; /* TRUE => a redirection was used */
  21. void SetupRedir()
  22. {
  23. int err;
  24. DWORD wnError;
  25. CHAR szRemoteName[256];
  26. DWORD BufferSize = sizeof(szRemoteName)/sizeof(szRemoteName[0]);
  27. if (*szNet == '\0' || _strcmpi(szNet, "None") == 0)
  28. return; /* don't try to make connection */
  29. if(szPass) {
  30. strcpy(szNet + strlen(szNet) + 1, szPass);
  31. }
  32. if ( WN_SUCCESS != ( wnError = WNetGetConnection ( (PTSTR) szPName,
  33. szRemoteName,
  34. &BufferSize ) ) ) {
  35. if ( (wnError != WN_NOT_CONNECTED) &&
  36. (wnError != WN_CONNECTION_CLOSED) ) {
  37. switch( wnError ) {
  38. case WN_NO_NETWORK:
  39. Fatal("The network is not installed or workstation not started");
  40. break;
  41. case WN_BAD_LOCALNAME:
  42. Fatal("Invalid local name specified");
  43. break;
  44. case WN_EXTENDED_ERROR:
  45. Fatal("Extended error from WNetGetConnection" );
  46. break;
  47. default:
  48. Fatal("error from WNetGetConnection: %d",wnError );
  49. break;
  50. }
  51. }
  52. }
  53. else { /* no error */
  54. if ( !strcmp( szRemoteName, szNet ) ) {
  55. return;
  56. }
  57. }
  58. /* no redirection, so set one up */
  59. if ((err = SetPrnRedir(szPName, szNet)) == 0)
  60. fRedir = TRUE;
  61. else if (err == 1)
  62. Fatal("redirection of %s to %s failed (%d)\nredirector is not started",
  63. szPName,szNet,err);
  64. else if (err != ERROR_ALREADY_ASSIGNED)
  65. Fatal("redirection of %s to %s failed (%d)",
  66. szPName,szNet,err);
  67. }
  68. void ResetRedir()
  69. {
  70. int err;
  71. if (fRedir)
  72. {
  73. if ((err = EndRedir(szPName)) != 0)
  74. Error("removal of redirection failed (%d)", err);
  75. fRedir = FALSE;
  76. }
  77. }
  78. int EndRedir(szDev)
  79. /* End redirection for szDev, return 0 if success */
  80. /* otherwise return error number */
  81. char * szDev;
  82. {
  83. DWORD wnError;
  84. if (WN_SUCCESS != ( wnError = WNetCancelConnection ( szDev, TRUE) ) ) {
  85. return ( 0 );
  86. }
  87. else {
  88. return ( (int) wnError );
  89. }
  90. }
  91. int SetPrnRedir(szDev, szPath)
  92. char *szDev;
  93. char *szPath;
  94. {
  95. DWORD wnError;
  96. /* return 0, 1 or other error */
  97. if ( szPass && *szPass ) {
  98. wnError = WNetAddConnection ( (LPTSTR) szPath, (LPTSTR) szPass, (LPTSTR) szDev );
  99. } else {
  100. wnError = WNetAddConnection ( (LPTSTR) szPath, NULL, (LPTSTR) szDev );
  101. }
  102. if(wnError == WN_SUCCESS) {
  103. wnError = 0;
  104. } else if((wnError == WN_NO_NETWORK) || (wnError == WN_NO_NET_OR_BAD_PATH)) {
  105. wnError = 1;
  106. }
  107. return ( (int)wnError );
  108. }
  109. BOOL QueryUserName(char *szName)
  110. {
  111. UCHAR pUserName [128];
  112. DWORD BufferSize = 128;
  113. BOOL rc;
  114. DWORD wnError;
  115. if (WN_SUCCESS != ( wnError = WNetGetUser ( NULL, (LPTSTR) pUserName, &BufferSize ) ) ) {
  116. strcpy(szName,"no name");
  117. rc = FALSE;
  118. if(fVerify) {
  119. fprintf(stdout,"Warning: WNetGetUser returns %d\n", wnError );
  120. }
  121. } else {
  122. strcpy(szName, pUserName);
  123. rc = TRUE;
  124. }
  125. return(rc);
  126. }