信息来源:refdom
opentelnet.cpp- ////////////////////////////////////////////////////////////////////////////////
- //
- // Resume Telnet Remote Config
- //
- // File : ReumeTelnet.cpp
- // Version : 0.5
- // Comment : only for win2k and XP(pro\server\adv)
- //
- // Create at : 2002.3.9
- // Create by : refdom
- // Email : [email]refdom@263.net[/email]
- // Home Page : [url]www.opengram.com[/url]
- //
- // If you modify the code, or add more functions, please email me a copy.
- //
- ////////////////////////////////////////////////////////////////////////////////
- #include <stdio.h>
- #include <windows.h>
- #pragma comment(lib, "Advapi32.lib")
- #pragma comment(lib, "Mpr.lib")
- SC_HANDLE g_schSCManager;
- SC_HANDLE g_schRegistryService;
- HKEY g_hKey;
- DWORD g_DefaultTelnetStartType = 0;
- DWORD g_DefaultRegistryStartType = 0;
- LPBYTE g_lpDefaultTelnetNTLM;
- LPBYTE g_lpDefaultTelnetPort;
- void Usage(char*);
- int CloseTelnet();
- int StartRemoteRegistry();
- int MyStartService(SC_HANDLE, char*);
- int ResumeRegistryService();
- int main(int argc, char* argv[])
- {
- int nRetCode;
- char szIpc[50] = "";
- HKEY hKey;
- LPSTR lpUserName, lpPassword;
- NETRESOURCE NET;
- Usage(argv[0]);
- if (argc != 4)
- return 0;
- sprintf (szIpc, "%s\\ipc$", argv[1]);
- lpUserName = argv[2];
- lpPassword = argv[3];
- NET.lpLocalName = NULL;
- NET.lpRemoteName = szIpc;
- NET.dwType = RESOURCETYPE_ANY;
- NET.lpProvider = NULL;
- printf ("Connecting %s...",argv[1]);
- ReConnect:
- nRetCode = WNetCancelConnection2(szIpc, CONNECT_UPDATE_PROFILE, TRUE);
- if (nRetCode == NO_ERROR)
- printf ("Canncel Successfully!\n");
- nRetCode = WNetAddConnection2(&NET, lpPassword, lpUserName, CONNECT_INTERACTIVE);
- if (nRetCode == ERROR_ALREADY_ASSIGNED || nRetCode == ERROR_DEVICE_ALREADY_REMEMBERED)
- {
- printf ("Already conneted to the server!\n");
- printf ("Now re-connecting the server...\n");
- goto ReConnect;
- }
- else if (nRetCode == NO_ERROR)
- printf ("Successfully!\n");
- else
- {
- printf ("\n\tErr:");
- switch (nRetCode)
- {
- case ERROR_ACCESS_DENIED:
- printf ("ERROR_ACCESS_DENIED\n");
- break;
- case ERROR_BAD_NET_NAME:
- printf ("ERROR_BAD_NET_NAME\n");
- break;
- default:
- printf ("CONNECT ERR:%d!\n",GetLastError());
- break;
- }
- return 0;
- }
- //open SCManager
- g_schSCManager = OpenSCManager(argv[1], NULL, SC_MANAGER_ALL_ACCESS);
- if (g_schSCManager == NULL)
- {
- printf ("Open SCManager failed!\n");
- return 0;
- }
- //check remote registry service is running
- if (!StartRemoteRegistry())
- {
- printf ("All Process Failed!\n");
- return 0;
- }
- //edit the registry
- if (!(RegConnectRegistry((LPCTSTR) argv[1], HKEY_LOCAL_MACHINE, &g_hKey) == ERROR_SUCCESS))
- {
- printf ("Connect remote registry failed!\n");
- return 0;
- }
- if (!(RegOpenKeyEx(g_hKey, "SOFTWARE\\Microsoft\\TelnetServer\\1.0", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS))
- {
- printf ("Open key failed!\n");
- return 0;
- }
- //read the registry for default config
- g_lpDefaultTelnetNTLM = (LPBYTE) LocalAlloc(LPTR, 50);
- g_lpDefaultTelnetPort = (LPBYTE) LocalAlloc(LPTR, 50);
- DWORD dwDataSize = 50;
- if (!(RegQueryValueEx(hKey, "default_NTLM", NULL, NULL, g_lpDefaultTelnetNTLM, &dwDataSize) == ERROR_SUCCESS))
- {
- printf ("Read NTLM failed!\n ");
- printf ("%d\n", GetLastError());
- return 0;
- }
- if (!(RegQueryValueEx(hKey, "default_Port", NULL, NULL, g_lpDefaultTelnetPort, &dwDataSize) == ERROR_SUCCESS))
- {
- printf ("Read port failed!\n ");
- return 0;
- }
- if (!(RegQueryValueEx(hKey, "default_RegistryStart", NULL, NULL, (LPBYTE) &g_DefaultRegistryStartType, &dwDataSize) == ERROR_SUCCESS))
- {
- printf ("Read registrystart failed!\n ");
- return 0;
- }
- if (!(RegQueryValueEx(hKey, "default_TelnetStart", NULL, NULL, (LPBYTE) &g_DefaultTelnetStartType, &dwDataSize) == ERROR_SUCCESS))
- {
- printf ("Read telnetstart failed!\n ");
- return 0;
- }
- if (!(RegSetValueEx(hKey, "NTLM", 0, REG_DWORD, g_lpDefaultTelnetNTLM, sizeof(DWORD)) == ERROR_SUCCESS))
- {
- printf ("Set NTLM failed!");
- return 0;
- }
- if (!(RegSetValueEx(hKey, "TelnetPort", 0, REG_DWORD, g_lpDefaultTelnetPort, sizeof(DWORD)) == ERROR_SUCCESS))
- {
- printf ("Set port failed!");
- return 0;
- }
- //close telnet service
- nRetCode = CloseTelnet();
- if (nRetCode)
- {
- printf ("\nBINGLE!!!\n");
- printf ("The config of remote telnet server is resumed!\n");
- }
- //resume the configure
- if (!(RegDeleteValue(hKey, (LPCTSTR) "default_NTLM") == ERROR_SUCCESS))
- printf ("Delete NTLM value failed!\n");
- if (!(RegDeleteValue(hKey, (LPCTSTR) "default_Port") == ERROR_SUCCESS))
- printf ("Delete port value failed!\n");
- if (!(RegDeleteValue(hKey, (LPCTSTR) "default_RegistryStart") == ERROR_SUCCESS))
- printf ("Delete registrystart value failed!\n");
- if (!(RegDeleteValue(hKey, (LPCTSTR) "default_TelnetStart") == ERROR_SUCCESS))
- printf ("Delete telnetstart value failed!\n");
- RegCloseKey(hKey);
- RegCloseKey(g_hKey);
- ResumeRegistryService();
- CloseServiceHandle(g_schRegistryService);
- //close SCManager
- CloseServiceHandle(g_schSCManager);
- //close the session with remote server
- printf ("\nDisconnecting server...");
- nRetCode = WNetCancelConnection2(argv[1], CONNECT_UPDATE_PROFILE, TRUE);
- if (nRetCode == NO_ERROR)
- printf ("Successfully!\n");
- else
- printf ("Failed!\n");
- LocalFree(g_lpDefaultTelnetNTLM);
- LocalFree(g_lpDefaultTelnetPort);
- return 0;
- }
- void Usage(char* pcAppName)
- {
- printf ("*******************************************************\n");
- printf ("Resume Remote Telnet Config, by refdom\n");
- printf ("Email: [email]refdom@263.net[/email]\n");
- printf ("%s\n\n", pcAppName);
- printf ("Usage:ResumeTelnet.exe \\\\server username password\n");
- printf ("*******************************************************\n");
- return;
- }
- int CloseTelnet()
- {
- DWORD dwWaitTime;
- DWORD dwConfigSize;
- SC_HANDLE schTelnetService;
- SERVICE_STATUS ssTelnetStatus;
- LPQUERY_SERVICE_CONFIG lpTelnetConfig;
- //stop the telnet service
- schTelnetService = OpenService(g_schSCManager, "TlntSvr", SERVICE_ALL_ACCESS);
- if (schTelnetService == NULL)
- {
- printf ("Open service failed!\n");
- return 0;
- }
- lpTelnetConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
- if (lpTelnetConfig == NULL)
- {
- printf ("Alloc memory failed!\n");
- return 0;
- }
- if (!QueryServiceConfig(schTelnetService, lpTelnetConfig, 1024, &dwConfigSize))
- {
- printf ("Query service congfig failed!\n");
- return 0;
- }
- if (lpTelnetConfig->dwStartType != g_DefaultTelnetStartType)
- {
- if (!ChangeServiceConfig(schTelnetService,
- SERVICE_NO_CHANGE,
- g_DefaultTelnetStartType,
- SERVICE_NO_CHANGE,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL))
- {
- printf ("Set telnet start type error!\n");
- return 0;
- }
- }
- if (!(QueryServiceStatus(schTelnetService, &ssTelnetStatus)))
- {
- printf ("Query service status failed!\n");
- return 0;
- }
- if (ssTelnetStatus.dwCurrentState != SERVICE_STOPPED && ssTelnetStatus.dwCurrentState != SERVICE_STOP_PENDING)
- {
- printf ("Stopping telnet service ...\n");
- if (!(ControlService(schTelnetService, SERVICE_CONTROL_STOP, &ssTelnetStatus)))
- {
- printf ("Control telnet service status failed!\n");
- return 0;
- }
- dwWaitTime = ssTelnetStatus.dwWaitHint / 10;
- if( dwWaitTime < 1000 )
- dwWaitTime = 1000;
- else if ( dwWaitTime > 10000 )
- dwWaitTime = 10000;
- Sleep(dwWaitTime);
- if (!QueryServiceStatus(schTelnetService, &ssTelnetStatus))
- {
- printf ("Query service status failed!\n");
- }
- if ( ssTelnetStatus.dwCurrentState == SERVICE_STOPPED || ssTelnetStatus.dwCurrentState == SERVICE_STOP_PENDING)
- {
- printf ("Telnet service is stopped successfully!\n");
- }
- else
- {
- printf ("Stopping telnet service failed!\n");
- return 0;
- }
- }
- LocalFree(lpTelnetConfig);
- CloseServiceHandle(schTelnetService);
- return 1;
- }
- int StartRemoteRegistry()
- {
- SC_HANDLE schRegistryService;
- SERVICE_STATUS ssRegistryStatus;
- schRegistryService = OpenService( g_schSCManager, "RemoteRegistry", SERVICE_ALL_ACCESS);
- g_schRegistryService = OpenService( g_schSCManager, "RemoteRegistry", SERVICE_ALL_ACCESS);;
- if (schRegistryService == NULL || g_schRegistryService == NULL)
- {
- printf ("Open remote registry service failed!\n");
- return 0;
- }
- if (!QueryServiceStatus(schRegistryService, &ssRegistryStatus))
- {
- printf ("Query remote registry service failed!\n");
- return 0;
- }
- if (ssRegistryStatus.dwCurrentState != SERVICE_RUNNING)
- {
- if (!MyStartService(schRegistryService, "remote registry"))
- return 0;
- }
- CloseServiceHandle(schRegistryService);
- return 1;
- }
- int MyStartService(SC_HANDLE schService, char* szServiceName)
- {
- DWORD dwWaitTime;
- DWORD dwOldCheckPoint;
- DWORD dwStartTickCount;
- SERVICE_STATUS ssStatus;
- printf ("Starting %s service...\n", szServiceName);
- if (!(StartService(schService, 0, NULL)))
- {
- printf ("Starting %s service failed!\n", szServiceName);
- return 0;
- }
- if (!(QueryServiceStatus(schService, &ssStatus)))
- {
- printf ("Query %s service status failed!\n",szServiceName);
- // return ;
- }
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- while ( ssStatus.dwCurrentState == SERVICE_START_PENDING)
- {
- dwWaitTime = ssStatus.dwWaitHint / 10;
- if( dwWaitTime < 1000 )
- dwWaitTime = 1000;
- else if ( dwWaitTime > 10000 )
- dwWaitTime = 10000;
- Sleep(dwWaitTime);
- // Check the status again.
- if (!QueryServiceStatus(schService, &ssStatus))
- break;
- if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
- {
- // The service is making progress.
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- }
- else
- {
- if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
- {
- // No progress made within the wait hint
- break;
- }
- }
- }
- if ( ssStatus.dwCurrentState == SERVICE_RUNNING )
- {
- printf ("%s service is started successfully! %s service is running!\n", szServiceName, szServiceName);
- }
- else
- {
- printf ("%s service is not started!\n", szServiceName);
- return 0;
- }
- return 1;
- }
- int ResumeRegistryService()
- {
- LPQUERY_SERVICE_CONFIG lpRegistryConfig;
- DWORD dwConfigSize;
- lpRegistryConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
- if (lpRegistryConfig == NULL)
- {
- printf ("Alloc memory failed!\n");
- return 0;
- }
- if (!QueryServiceConfig(g_schRegistryService, lpRegistryConfig, 1024, &dwConfigSize))
- {
- printf ("Query registry service congfig failed!\n");
- printf ("%d\n",GetLastError());
- return 0;
- }
- if (lpRegistryConfig->dwStartType != g_DefaultRegistryStartType)
- {
- if (!ChangeServiceConfig(g_schRegistryService,
- SERVICE_NO_CHANGE,
- g_DefaultRegistryStartType,
- SERVICE_NO_CHANGE,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL))
- {
- printf ("Set registry start type error!\n");
- return 0;
- }
- }
- LocalFree(lpRegistryConfig);
- return 1;
- }
复制代码 |