1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
| void cb_send_cmd(GtkWidget *widget, gpointer user_data)
{
//SOCKET remote_shell_sock = 0;
//GtkWidget *remote_shell_closed_dialog = NULL;
GtkTextBuffer *text_buffer = NULL;
gchar *text = NULL;
GtkTextIter start;
GtkTextIter end;
gchar *utf8_text = NULL;
FILE *log_cmd_output = NULL;
FILE *log_cmd_historic = NULL;
//char *buffer = NULL;
const char *buffer_cmd = NULL;
//char *log_buffer = NULL;
//char *historic_buffer = NULL;
char *start_command = "> Command : ";
char historic[MAXDATASIZE] = {""};
char cmd_output[MAXDATASIZE] = {""};
int caractereLu = 0;
int i = 0;
char line[MAXDATASIZE] = {""};
char line_output[MAXDATASIZE] = {""};
char *print_line = NULL;
char *print_line_output = NULL;
size_t data_len = 0;
sended_one_cmd = 1;
buffer_cmd = gtk_entry_get_text(GTK_ENTRY(user_data));
log_cmd_historic = fopen("Logs/cmd_historic.log", "a");
if(log_cmd_historic == NULL)
{
error("fopen() log_cmd_historic", "cb_send_cmd()");
return;
}
fputs("> Command : ", log_cmd_historic);
fputs(buffer_cmd, log_cmd_historic);
fputs("\n", log_cmd_historic);
fclose(log_cmd_historic);
log_cmd_output = fopen("Logs/remote_shell.log", "a");
if(log_cmd_output == NULL)
{
error("fopen() log_cmd_output", "cb_send_cmd()");
return;
}
// Write in log file the command name
fputs("> Command : ", log_cmd_output);
fputs(buffer_cmd, log_cmd_output);
fputs("\n", log_cmd_output);
var_allocation();
/*
buffer = malloc(BUFSIZ * sizeof(char));
if(buffer == NULL)
{
error("malloc() buffer", "send_cmd()");
return;
}
log_buffer = malloc(MAXDATASIZE * sizeof(char));
if(log_buffer == NULL)
{
error("malloc log_buffer", "cb_send_cmd()");
return;
}
historic_buffer = malloc(MAXDATASIZE * sizeof(char));
if(historic_buffer == NULL)
{
error("malloc historic_buffer", "cb_send_cmd()");
return;
}
*/
data_len = strlen(buffer_cmd) + 1;
// Send the command length
if(send(sock, (char*)&data_len, sizeof(data_len), 0) == SOCKET_ERROR)
{
error("send() data_len", "cb_send_cmd()");
return;
}
// Send the command
if(send(sock, buffer_cmd, data_len, 0) == SOCKET_ERROR)
{
error("send() buffer_cmd", "cb_send_cmd()");
return;
}
if(strncmp(buffer_cmd, "quit", 4) == 0)
{
gtk_entry_set_text(GTK_ENTRY(user_data), "Use quit command for exit ...");
if(system("rm -rf Logs/cmd_historic.log") == -1)
{
error("rm -rf Logs/cmd_historic.log in quit", "cb_send_cmd()");
return;
}
if(system("rm -rf Logs/remote_shell.log") == -1)
{
error("rm -rf Logs/remote_shell.log in quit", "cb_send_cmd()");
return;
}
//shutdown(remote_shell_sock, SHUT_WR);
remote_shell_closed_dialog = gtk_message_dialog_new (GTK_WINDOW(main_win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Remote Shell have been closed !");
switch(gtk_dialog_run(GTK_DIALOG(remote_shell_closed_dialog)))
{
default :
gtk_widget_destroy(remote_shell_closed_dialog);
break;
}
return;
}
/** PROBLEM DE RECEPTION DE LA COMMANDE **/
// Receive the command output
if(recv(sock, buffer, BUFSIZ, 0) == SOCKET_ERROR)
{
error("recv() buffer", "cb_send_cmd()");
return;
}
printf("\n\nCommand output received : %s", buffer);
printf("\n\n");
// Write in log file the command output
fputs(buffer, log_cmd_output);
fputs("\n", log_cmd_output);
fclose(log_cmd_output);
// Fill buffer from print in command's historic
utf8_text = g_locale_to_utf8(start_command, strlen(start_command), NULL, NULL, NULL);
historic_buffer = strncpy(historic_buffer, utf8_text, strlen(utf8_text) + 1);
historic_buffer = strncat(historic_buffer, buffer_cmd, strlen(buffer_cmd) + 1);
historic_buffer = strncat(historic_buffer, "\n", 2);
// Print output command in historic
log_cmd_historic = fopen("Logs/cmd_historic.log", "r");
if(log_cmd_historic == NULL)
{
error("fopen() cmd_historic opened with r", "cb_send_cmd()");
return;
}
utf8_text = g_locale_to_utf8(historic, strlen(historic), NULL, NULL, NULL);
while((caractereLu = fgetc(log_cmd_historic)) != EOF)
{
historic[i] = (char)caractereLu;
i++;
}
print_line = malloc(MAXDATASIZE * sizeof(char));
if(print_line == NULL)
{
error("malloc() print_line", "cb_send_cmd()");
return;
}
fseek(log_cmd_historic, 0, SEEK_SET);
if(log_cmd_historic != NULL)
{
strncpy(print_line, line, strlen(print_line) + 1);
while (fgets(line, MAXDATASIZE, log_cmd_historic) != NULL)
{
strncat(print_line, line, strlen(line));
}
fclose(log_cmd_historic);
}
utf8_text = g_locale_to_utf8(print_line, strlen(print_line), NULL, NULL, NULL);
// Obtaining the buffer associated with the widget
text_buffer = gtk_text_view_get_buffer((GtkTextView*)(historic_text_view));
// Set the default buffer text.
gtk_text_buffer_set_text(text_buffer, utf8_text, -1);
// Obtain iters for the start and end of points of the buffer
gtk_text_buffer_get_start_iter(text_buffer, &start);
gtk_text_buffer_get_end_iter(text_buffer, &end);
// Get the entire buffer text
text = gtk_text_buffer_get_text(text_buffer, &start, &end, FALSE);
// Print the text
g_print("%s", text);
g_free(text);
// Prepare for print command out put
log_cmd_output = fopen("Logs/remote_shell.log", "r");
if(log_cmd_output == NULL)
{
error("fopen() log_cmd_output opened with r", "cb_send_cmd()");
return;
}
utf8_text = g_locale_to_utf8(cmd_output, strlen(cmd_output), NULL, NULL, NULL);
while((caractereLu = fgetc(log_cmd_output)) != EOF)
{
cmd_output[i] = (char)caractereLu;
i++;
}
print_line_output = malloc(MAXDATASIZE * sizeof(char));
if(print_line_output == NULL)
{
error("malloc() print_line_output", "cb_send_cmd()");
return;
}
fseek(log_cmd_output, 0, SEEK_SET);
if(log_cmd_output != NULL)
{
strncpy(print_line_output, line_output, strlen(print_line_output) + 1);
while (fgets(line_output, MAXDATASIZE, log_cmd_output) != NULL)
{
strncat(print_line_output, line_output, strlen(line_output));
}
fclose(log_cmd_output);
}
// Print outpout command in log
// Copy the command output
utf8_text = g_locale_to_utf8(print_line_output, strlen(print_line_output), NULL, NULL, NULL);
// Obtaining the buffer associated with the widget
text_buffer = gtk_text_view_get_buffer((GtkTextView*)(text_view));
// Set the default buffer text.
gtk_text_buffer_set_text(text_buffer, utf8_text, -1);
// Obtain iters for the start and end of points of the buffer
gtk_text_buffer_get_start_iter(text_buffer, &start);
gtk_text_buffer_get_end_iter(text_buffer, &end);
// Get the entire buffer text
text = gtk_text_buffer_get_text(text_buffer, &start, &end, FALSE);
// Print the text
g_print("%s", text);
g_free(text);
free(buffer);
free (log_buffer);
free (historic_buffer);
// Parametres inutilises
(void)widget;
} |
Partager