-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathremote-inputd.c
More file actions
272 lines (226 loc) · 7.19 KB
/
Copy pathremote-inputd.c
File metadata and controls
272 lines (226 loc) · 7.19 KB
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
/*
* Copyright (C) 2016 Ingemar Ådahl
*
* This file is part of remote-input.
*
* remote-input is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* remote-input is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with remote-input. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <signal.h>
#include <syslog.h>
#include <sys/wait.h>
#include "input_device.h"
#include "logging.h"
#include "server.h"
#include "shared.h"
#include "out/gen/keymap.h"
#define INPUT_DEVICE_NAME "remote-input"
#define DEFAULT_PORT_NUMBER 4004
#define UNPRIVILEGED_USER "nobody"
#define FATAL_ERRNO(format) { \
LOG_ERRNO(format); \
exit(EXIT_FAILURE); \
}
/* Modified by signal handler, indicating that the main loop should exit */
static volatile sig_atomic_t should_exit = false;
struct args {
bool dont_daemonize;
int verbosity;
uint16_t local_port;
char* local_host;
};
static const struct args argument_defaults = {
.dont_daemonize = false,
.verbosity = LOG_NOTICE,
.local_port = DEFAULT_PORT_NUMBER,
.local_host = NULL
};
static void sig_handler(int signum) {
switch (signum) {
case SIGINT:
case SIGTERM:
should_exit = true;
break;
}
}
static int install_signal_handlers(void) {
static struct sigaction sa = {
.sa_handler = sig_handler
};
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
return 0;
}
static void daemonize(void) {
pid_t pid = fork();
if (pid < 0) FATAL_ERRNO("couldn't fork");
if (pid > 0) {
LOG(INFO, "forked daemon with pid [%d]", pid);
/* Wait for the child process to become the session leader, or else the
* parent might take it down on exit */
int child_status;
waitpid(pid, &child_status, 0);
exit(WEXITSTATUS(child_status));
}
if (setsid() < 0) {
FATAL_ERRNO("couldn't become a daemon");
}
log_set_target(SYSLOG);
fclose(stdin);
fclose(stdout);
fclose(stderr);
pid = fork();
if (pid < 0) FATAL_ERRNO("couldn't fork");
if (pid > 0) {
LOG(INFO, "forked again to [%d]", pid);
exit(EXIT_SUCCESS);
}
}
static void drop_privileges(void) {
struct passwd* unprivileged_user = getpwnam(UNPRIVILEGED_USER);
if (unprivileged_user == NULL) {
LOG(ERROR, "couldn't find user \"%s\"", UNPRIVILEGED_USER);
exit(EXIT_FAILURE);
}
if(setgid(unprivileged_user->pw_gid) < 0) {
FATAL_ERRNO("couldn't change group");
}
if(setuid(unprivileged_user->pw_uid) < 0) {
FATAL_ERRNO("couldn't change user");
}
}
static void handle_event(struct input_device* device,
struct client_event* event) {
switch (event->type) {
case EV_MOUSE_DX:
device_mouse_move(device, event->value, 0);
break;
case EV_MOUSE_DY:
device_mouse_move(device, 0, event->value);
break;
case EV_KEY_DOWN:
device_key_down(device, lookup_keycode(event->value));
break;
case EV_KEY_UP:
device_key_up(device, lookup_keycode(event->value));
break;
case EV_WHEEL:
device_mouse_wheel(device, 0, event->value);
break;
case EV_HWHEEL:
device_mouse_wheel(device, event->value, 0);
break;
default:
LOG(ERROR, "unknown event type: %u", event->type);
}
}
static void handle_client(struct client_info* client,
struct input_device* device) {
struct client_event event;
while (read_client_event(client, &event) > 0) {
handle_event(device, &event);
}
device_release_all_keys(device);
LOG(NOTICE, "terminating connection from %s", client->cl_addr);
close(client->cl_fd);
}
static void usage(const char* program_name) {
printf("Usage: %s [OPTION]\n", program_name);
printf("\nOptions:\n"
" -d don't detach and do not become a daemon\n"
" -l hostname/ip hostname or ip on which to listen on\n"
" -p port_number "
"specify which port to bind to (defaults to %u)\n"
" -v --verbose increase verbosity/logging level\n"
" -h --help show this help text and exit\n"
, DEFAULT_PORT_NUMBER);
}
static struct args parse_args(int argc, char* argv[]) {
struct args args = argument_defaults;
struct option const long_options[] = {
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
int ch;
while ((ch = getopt_long(argc, argv, "dvhl:p:", long_options, NULL)) > 0) {
switch (ch) {
case 'd':
args.dont_daemonize = true;
break;
case 'v':
if (args.verbosity < LOG_DEBUG) {
args.verbosity++;
}
break;
case 'l':
args.local_host = optarg;
break;
case 'p':
{
int port = strtol(optarg, NULL, 10);
if (port < 1 || port > UINT16_MAX) {
LOG(ERROR, "bad port number: %s", optarg);
exit(EXIT_FAILURE);
}
args.local_port = (uint16_t) port;
}
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
return args;
}
int main(int argc, char* argv[]) {
struct args args = parse_args(argc, argv);
log_set_level(args.verbosity);
struct input_device device;
if (device_create(INPUT_DEVICE_NAME, &device) < 0) {
LOG(FATAL, "couldn't create input device");
exit(EXIT_FAILURE);
}
install_signal_handlers();
struct server_info server;
if (server_create(args.local_host, args.local_port, &server) < 0) {
exit(EXIT_FAILURE);
}
LOG(NOTICE, "listening for connections on %s:%d", server.sv_addr,
server.sv_port);
drop_privileges();
/* Wait until after server creation, making sure errors are obvious */
if (!args.dont_daemonize) {
daemonize();
}
struct client_info client;
while (!should_exit && server_accept(&server, &client) != -1) {
LOG(NOTICE, "accepted connection from %s", client.cl_addr);
handle_client(&client, &device);
}
server_close(&server);
device_close(&device);
LOG(INFO, "terminating successfully");
return 0;
}