hardcode c:\reactos\system32\cmd.exe for now
[reactos.git] / rosapps / applications / sysutils / telnetd / telnetd.c
index d8d0e98..6dbd20d 100644 (file)
  * Use freely, no copyrights.
  * Use Linux.
  *
+ * Parts Copyright Steven Edwards
+ * Public Domain
+ *
  * TODO: 
  * - access control
  * - will/won't handshake
  * - Unify Debugging output and return StatusCodes
  */
 
-#include <stdio.h> 
-#include <windows.h>
-
 #include "telnetd.h"
 
 #define telnetd_printf printf
-
 #if 0
-extern void syslog (int priority, const char *fmt, ...);
-
-int telnetd_printf(const char *format, ...)
+static inline int telnetd_printf(const char *format, ...);
 {
- syslog (6, format);
+    printf(format,...);
+    syslog (6, format);
 }
 #endif
 
@@ -37,7 +35,6 @@ int telnetd_printf(const char *format, ...)
 
 static BOOLEAN bShutdown = 0;
 static BOOLEAN bSocketInterfaceInitialised = 0;
-
 static int sock;
 
 /* In the future, some options might be passed here to handle
@@ -50,6 +47,9 @@ static int sock;
  */
 int main(int argc, char **argv)
 {
+  printf("Attempting to start Simple TelnetD\n");
+
+//  DetectPlatform();
   SetConsoleCtrlHandler(Cleanup, 1);
 
   if (!StartSocketInterface())
@@ -143,7 +143,6 @@ static void UserLogin(int client_socket)
   if (client == NULL)
     ErrorExit("failed to allocate memory for client");
 
-
   client->socket = client_socket;
   CreateThread(NULL, 0, UserLoginThread, client, 0, &threadID);
 }
@@ -365,7 +364,7 @@ static void RunShell(client_t *client)
    PROCESS_INFORMATION   piProcInfo;
    SECURITY_ATTRIBUTES   saAttr;
 
-   const char *name = "c:\\windows\\system32\\cmd.exe";
+   const char *name = "c:\\reactos\\system32\\cmd.exe";
    const char *cmd = NULL;
    //const char *name = "d:\\cygwin\\bin\\bash.exe";
    //const char *cmd = "d:\\cygwin\\bin\\bash.exe --login -i";
@@ -574,35 +573,54 @@ static DWORD WINAPI ReadFromPipeThread(LPVOID data)
 /* TerminateShell */ 
 static void TerminateShell(client_t *client)
 {
-  DWORD exitCode;
-  DWORD dwWritten;
-  char stop[] = "\003\r\nexit\r\n"; /* Ctrl-C + exit */
+    DWORD exitCode;
+    DWORD dwWritten;
+    char stop[] = "\003\r\nexit\r\n"; /* Ctrl-C + exit */
 
-  GetExitCodeProcess(client->hProcess, &exitCode);
-  if (exitCode == STILL_ACTIVE) {
-    telnetd_printf("user shell still active, send Ctrl-Break to group-id %lu\n", client->dwProcessId );
+    GetExitCodeProcess(client->hProcess, &exitCode);
 
-    if (!GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, client->dwProcessId ))
-      telnetd_printf("Failed to send Ctrl_break\n");
+    if (exitCode == STILL_ACTIVE)
+    {
+        HANDLE hEvent = NULL;
+        DWORD dwWaitResult;
 
-    Sleep(500);
+        telnetd_printf("user shell still active, send Ctrl-Break to group-id %lu\n", client->dwProcessId );
 
-    if (!GenerateConsoleCtrlEvent( CTRL_C_EVENT, client->dwProcessId ))
-      telnetd_printf("Failed to send Ctrl_C\n");
+        hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
 
-    Sleep(500);
+        if (hEvent == NULL)
+            printf("CreateEvent error\n");
 
-    if (!WriteFile(client->hChildStdinWr, stop, sizeof(stop), &dwWritten, NULL))
-      telnetd_printf("Error writing to pipe\n");
+        if (!GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, client->dwProcessId ))
+            telnetd_printf("Failed to send Ctrl_break\n");
 
-    Sleep(500);
+        if (!GenerateConsoleCtrlEvent( CTRL_C_EVENT, client->dwProcessId ))
+            telnetd_printf("Failed to send Ctrl_C\n");
 
-    GetExitCodeProcess(client->hProcess, &exitCode);
-    if (exitCode == STILL_ACTIVE) {
-      telnetd_printf("user shell still active, attempt to terminate it now...\n");
-      TerminateProcess(client->hProcess, 0);
+        if (!WriteFile(client->hChildStdinWr, stop, sizeof(stop), &dwWritten, NULL))
+            telnetd_printf("Error writing to pipe\n");
+
+        /* wait for our handler to be called */
+        dwWaitResult=WaitForSingleObject(hEvent, 500);
+
+        if (WAIT_FAILED==dwWaitResult)
+            telnetd_printf("WaitForSingleObject failed\n");
+
+        GetExitCodeProcess(client->hProcess, &exitCode);
+        if (exitCode == STILL_ACTIVE) 
+        {
+            telnetd_printf("user shell still active, attempt to terminate it now...\n");
+        
+            if (hEvent != NULL) 
+            {
+                if (!CloseHandle(hEvent)) 
+                   telnetd_printf("CloseHandle");
+            }
+            TerminateProcess(client->hProcess, 0);
+        }
+        TerminateProcess(client->hProcess, 0);
     }
-  }
+    TerminateProcess(client->hProcess, 0);
 }
 
 /* ErrorExit */