1 /* Ssh.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all copies or substantial portions
11 of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19
20 /****************************************************************
21 * *
22 * Execute a shell command in remote host *
23 * plink - Remote Shell Client for Windows system *
24 * ssh - Remote Shell Client using ssh system *
25 * command for a unix/Linux system *
26 * *
27 ****************************************************************/
28 #include "../../Config.h"
29 #include <glib.h> /* definition of G_OS_WIN32 if windows */
30 #include "../Common/Global.h"
31 #include "../Utils/UtilsInterface.h"
32 #include "../Utils/Utils.h"
33
34 #ifdef G_OS_WIN32
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <winsock.h>
40 #include <fcntl.h>
41 #include <io.h>
42
43 static gchar* cmdGlobal = NULL;
44 static gchar* foutGlobal = NULL;
45 static gchar* ferrGlobal = NULL;
46 static gchar* userNameGlobal = NULL;
47 static gchar* rhostGlobal = NULL;
48 static gchar* passWordGlobal = NULL;
49
clientThread()50 static long clientThread ()
51 {
52 gchar *command;
53 #ifndef G_OS_WIN32
54 FILE* f = stdout;
55 #endif
56 /*
57 FILE* FileOut =freopen(foutGlobal, "w", stdout);
58
59 */
60 FILE* FileErr =freopen(ferrGlobal, "w", stderr);
61 gchar* fileouttmp =
62 g_strdup_printf("%s%stmp%sfouttmp",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
63
64
65
66 command = g_strdup_printf(
67 "%s -pw %s %s@%s %s > \"%s\"",
68 plinkCommand,passWordGlobal,
69 userNameGlobal,rhostGlobal,cmdGlobal
70 ,fileouttmp);
71
72 /* printf("%s\n",command);*/
73 system(command);
74 g_free(command);
75
76 command = g_strdup_printf("copy \"%s\" \"%s\"",fileouttmp,foutGlobal);
77 system(command);
78 g_free(command);
79 g_free(fileouttmp);
80
81 /*
82 fclose(FileOut);
83 */
84 fclose(FileErr);
85
86
87
88 return 0;
89 }
90
91 /********************************************************
92 * ssh : main processing routine; connect to server, *
93 * pass command line and wait for results *
94 *********************************************************/
ssh(char * fout,char * ferr,const char * cmd,const char * rhost,const char * userName,const char * password)95 void ssh (char *fout,char *ferr,const char* cmd,
96 const char *rhost,const char* userName,
97 const char* password
98 )
99 {
100 HANDLE threadHnd;
101 DWORD threadID;
102 int i;
103
104 cmdGlobal = g_strdup(cmd);
105 foutGlobal = g_strdup(fout);
106 ferrGlobal = g_strdup(ferr);
107 userNameGlobal = g_strdup(userName);
108 rhostGlobal = g_strdup(rhost);
109 cmdGlobal = g_strdup(cmd);
110 passWordGlobal = g_strdup(password);
111
112 if(strstr(cmdGlobal,"&"))
113 for(i=0;i<(int)strlen(cmdGlobal);i++)
114 {
115 if(cmdGlobal[i]=='&')
116 cmdGlobal[i] = ' ';
117 }
118 /*
119 printf("cmd = %s\n",cmdGlobal);
120 printf("fout = %s\n",foutGlobal);
121 printf("ferr = %s\n",ferrGlobal);
122 */
123
124 unlink(fout);
125 unlink(ferr);
126
127 threadHnd=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)clientThread,
128 (LPVOID)0, 0, (LPDWORD)&threadID);
129 if(!threadHnd)
130 {
131 FILE* FileErr = FOpen(ferr,"w");
132 fprintf(FileErr,_("Cannot start client thread...\n"));
133 fclose(FileErr);
134 goto end;
135 }
136 /*printf("End start client thread cmd = %s\n",cmd);*/
137
138 if(!strstr(cmd,"&"))
139 if(threadHnd)
140 {
141 DWORD exitCode=0;
142 GetExitCodeThread(threadHnd, &exitCode);
143 while(exitCode==STILL_ACTIVE)
144 {
145 Sleep(50);
146 GetExitCodeThread(threadHnd, &exitCode);
147 }
148 CloseHandle(threadHnd);
149 }
150 if(strstr(cmd,"&") && threadHnd)
151 Sleep(500);
152
153 WSACleanup();
154
155 end :
156 return;
157 }
158 #else /* G_SO_WIN32 */
159 /********************************************************
160 * rsh : main processing routine; connect to server, *
161 * pass command line and wait for results *
162 *********************************************************/
163 #include <stdlib.h>
164 #include <unistd.h>
165
ssh(char * fout,char * ferr,const char * cmd,const char * rhost,const char * userName,const char * password)166 void ssh (char *fout,char *ferr,const char* cmd,
167 const char *rhost,const char* userName,
168 const char* password
169 )
170 {
171 gchar *command;
172
173 command = g_strdup_printf(
174 "sh -c \"sh -c 'ssh -l%s %s %s' >%s 2>%s\"",
175 userName,rhost,cmd,
176 fout,ferr);
177
178 unlink(fout);
179 unlink(ferr);
180 {int ierr = system(command);}
181 g_free(command);
182 }
183 #endif /* G_SO_WIN32 */
184 /*********************************************************/
185
186