1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-26 01:35:08 +00:00

TelnetServer: Accept arbitrary command with -c

For example, this allows you to do something like
`TelnetServer -c /usr/bin/nyancat` to have the TelnetServer run
`/usr/bin/nyancat` instead of `/bin/Shell`.
This commit is contained in:
Larkin Nickle 2019-09-28 16:31:43 -04:00 committed by Andreas Kling
parent 801fe7beac
commit ab67f74588

View file

@ -85,13 +85,17 @@ int main(int argc, char** argv)
int opt; int opt;
u16 port = 23; u16 port = 23;
while ((opt = getopt(argc, argv, "p:")) != -1) { const char* command = "";
while ((opt = getopt(argc, argv, "p:c:")) != -1) {
switch (opt) { switch (opt) {
case 'p': case 'p':
port = atoi(optarg); port = atoi(optarg);
break; break;
case 'c':
command = optarg;
break;
default: default:
fprintf(stderr, "Usage: %s [-p port]", argv[0]); fprintf(stderr, "Usage: %s [-p port] [-c command]", argv[0]);
exit(1); exit(1);
} }
} }
@ -104,7 +108,7 @@ int main(int argc, char** argv)
HashMap<int, NonnullRefPtr<Client>> clients; HashMap<int, NonnullRefPtr<Client>> clients;
int next_id = 0; int next_id = 0;
server->on_ready_to_accept = [&next_id, &clients, &server] { server->on_ready_to_accept = [&next_id, &clients, &server, command] {
int id = next_id++; int id = next_id++;
auto client_socket = server->accept(); auto client_socket = server->accept();
@ -120,7 +124,7 @@ int main(int argc, char** argv)
return; return;
} }
run_command(ptm_fd, ""); run_command(ptm_fd, command);
auto client = Client::create(id, move(client_socket), ptm_fd); auto client = Client::create(id, move(client_socket), ptm_fd);
client->on_exit = [&clients, id] { clients.remove(id); }; client->on_exit = [&clients, id] { clients.remove(id); };