~kennylevinsen/seatd

ref: e99e7d71f75394b68b581e86af4bdbdb43ee64ea seatd/seatd/client.c -rw-r--r-- 10.7 KiB
e99e7d71Kenny Levinsen client: Do not use SOL_SOCKET for LOCAL_PEERCRED 2 months ago
                                                                                
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#if defined(__FreeBSD__)
#include <sys/ucred.h>
#include <sys/un.h>
#endif

#include "client.h"
#include "linked_list.h"
#include "log.h"
#include "poller.h"
#include "protocol.h"
#include "seat.h"
#include "server.h"
#include "terminal.h"

static int get_peer(int fd, pid_t *pid, uid_t *uid, gid_t *gid) {
#if defined(__linux__)
	struct ucred cred;
	socklen_t len = sizeof cred;
	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) {
		return -1;
	}
	*pid = cred.pid;
	*uid = cred.uid;
	*gid = cred.gid;
	return 0;
#elif defined(__FreeBSD__)
	struct xucred cred;
	socklen_t len = sizeof cred;
	if (getsockopt(fd, 0, LOCAL_PEERCRED, &cred, &len) == -1) {
		return -1;
	}
	*pid = -1;
	*uid = cred.cr_uid;
	*gid = cred.cr_ngroups > 0 ? cred.cr_groups[0] : (gid_t)-1;
	return 0;
#else
	return -1;
#endif
}

struct client *client_create(struct server *server, int client_fd) {
	uid_t uid;
	gid_t gid;
	pid_t pid;

	if (get_peer(client_fd, &pid, &uid, &gid) == -1) {
		return NULL;
	}

	struct client *client = calloc(1, sizeof(struct client));
	if (client == NULL) {
		return NULL;
	}

	client->uid = uid;
	client->gid = gid;
	client->pid = pid;
	client->session = -1;
	client->server = server;
	client->connection.fd = client_fd;
	linked_list_init(&client->devices);
	linked_list_insert(&server->idle_clients, &client->link);
	return client;
}

void client_destroy(struct client *client) {
	assert(client);
	client->server = NULL;
	if (client->connection.fd != -1) {
		close(client->connection.fd);
		client->connection.fd = -1;
	}
	linked_list_remove(&client->link);
	if (client->seat != NULL) {
		seat_remove_client(client);
	}
	if (client->event_source != NULL) {
		event_source_fd_destroy(client->event_source);
		client->event_source = NULL;
	}
	connection_close_fds(&client->connection);
	assert(linked_list_empty(&client->devices));
	free(client);
}

static int client_flush(struct client *client) {
	int ret = connection_flush(&client->connection);
	if (ret == -1 && errno == EAGAIN) {
		event_source_fd_update(client->event_source, EVENT_READABLE | EVENT_WRITABLE);
	} else if (ret == -1) {
		return -1;
	}
	return 0;
}

static int client_send_error(struct client *client, int error_code) {
	struct proto_server_error errmsg = {
		.error_code = error_code,
	};
	struct proto_header errheader = {
		.opcode = SERVER_ERROR,
		.size = sizeof errmsg,
	};

	if (connection_put(&client->connection, &errheader, sizeof errheader) == -1 ||
	    connection_put(&client->connection, &errmsg, sizeof errmsg) == -1) {
		log_error("could not send error to client");
		return -1;
	}
	return 0;
}

static char *client_get_seat_name(struct client *client) {
	(void)client;
	// TODO: Look up seat for session.
	return "seat0";
}

static int handle_open_seat(struct client *client) {
	char *seat_name = client_get_seat_name(client);
	if (seat_name == NULL) {
		log_error("could not get name of target seat");
		return -1;
	}

	struct seat *seat = server_get_seat(client->server, seat_name);
	if (seat == NULL) {
		log_error("unable to find seat by name");
		return -1;
	}

	linked_list_remove(&client->link);
	if (seat_add_client(seat, client) == -1) {
		log_errorf("unable to add client to target seat: %s", strerror(errno));
		return -1;
	}
	linked_list_insert(&seat->clients, &client->link);

	size_t seat_name_len = strlen(seat_name);

	struct proto_server_seat_opened rmsg = {
		.seat_name_len = (uint16_t)seat_name_len,
	};
	struct proto_header header = {
		.opcode = SERVER_SEAT_OPENED,
		.size = sizeof rmsg + seat_name_len,
	};

	if (connection_put(&client->connection, &header, sizeof header) == -1 ||
	    connection_put(&client->connection, &rmsg, sizeof rmsg) == -1 ||
	    connection_put(&client->connection, seat_name, seat_name_len) == -1) {
		log_errorf("unable to write response: %s", strerror(errno));
		return -1;
	}

	seat_open_client(seat, client);
	return 0;
}

static int handle_close_seat(struct client *client) {
	if (client->seat == NULL) {
		log_error("protocol error: no seat associated with client");
		return -1;
	}

	linked_list_remove(&client->link);
	if (seat_remove_client(client) == -1) {
		log_error("unable to remove client from seat");
		return -1;
	}
	linked_list_insert(&client->server->idle_clients, &client->link);

	struct proto_header header = {
		.opcode = SERVER_SEAT_CLOSED,
		.size = 0,
	};

	if (connection_put(&client->connection, &header, sizeof header) == -1) {
		log_errorf("unable to write response: %s", strerror(errno));
		return -1;
	}

	return 0;
}

static int handle_open_device(struct client *client, char *path) {
	if (client->seat == NULL) {
		log_error("protocol error: no seat associated with client");
		return -1;
	}

	struct seat_device *device = seat_open_device(client, path);
	if (device == NULL) {
		log_errorf("could not open device: %s", strerror(errno));
		goto fail;
	}

	int dupfd = dup(device->fd);
	if (dupfd == -1) {
		log_errorf("could not dup fd: %s", strerror(errno));
		seat_close_device(client, device);
		goto fail;
	}

	if (connection_put_fd(&client->connection, dupfd) == -1) {
		log_errorf("unable to queue fd for sending: %s", strerror(errno));
		return -1;
	}

	struct proto_server_device_opened msg = {
		.device_id = device->device_id,
	};
	struct proto_header header = {
		.opcode = SERVER_DEVICE_OPENED,
		.size = sizeof msg,
	};

	if (connection_put(&client->connection, &header, sizeof header) == -1 ||
	    connection_put(&client->connection, &msg, sizeof msg) == -1) {
		log_errorf("unable to write response: %s", strerror(errno));
		return -1;
	}

	return 0;

fail:
	return client_send_error(client, errno);
}

static int handle_close_device(struct client *client, int device_id) {
	if (client->seat == NULL) {
		log_error("protocol error: no seat associated with client");
		return -1;
	}

	struct seat_device *device = seat_find_device(client, device_id);
	if (device == NULL) {
		log_error("no such device");
		errno = EBADF;
		goto fail;
	}

	if (seat_close_device(client, device) == -1) {
		log_errorf("could not close device: %s", strerror(errno));
		goto fail;
	}

	struct proto_header header = {
		.opcode = SERVER_DEVICE_CLOSED,
		.size = 0,
	};

	if (connection_put(&client->connection, &header, sizeof header) == -1) {
		log_errorf("unable to write response: %s", strerror(errno));
		return -1;
	}

	return 0;

fail:
	return client_send_error(client, errno);
}

static int handle_switch_session(struct client *client, int session) {
	if (client->seat == NULL) {
		log_error("protocol error: no seat associated with client");
		return -1;
	}

	if (seat_set_next_session(client, session) == -1) {
		goto error;
	}

	return 0;

error:
	return client_send_error(client, errno);
}

static int handle_disable_seat(struct client *client) {
	if (client->seat == NULL) {
		log_error("protocol error: no seat associated with client");
		return -1;
	}

	if (seat_ack_disable_client(client) == -1) {
		goto error;
	}

	return 0;

error:
	return client_send_error(client, errno);
}

static int client_handle_opcode(struct client *client, uint16_t opcode, size_t size) {
	int res = 0;
	switch (opcode) {
	case CLIENT_OPEN_SEAT: {
		if (size != 0) {
			log_error("protocol error: invalid open_seat message");
			return -1;
		}
		res = handle_open_seat(client);
		break;
	}
	case CLIENT_CLOSE_SEAT: {
		if (size != 0) {
			log_error("protocol error: invalid close_seat message");
			return -1;
		}
		res = handle_close_seat(client);
		break;
	}
	case CLIENT_OPEN_DEVICE: {
		char path[MAX_PATH_LEN];
		struct proto_client_open_device msg;
		if (sizeof msg > size || connection_get(&client->connection, &msg, sizeof msg) == -1 ||
		    sizeof msg + msg.path_len > size || msg.path_len > MAX_PATH_LEN) {
			log_error("protocol error: invalid open_device message");
			return -1;
		}
		if (connection_get(&client->connection, path, msg.path_len) == -1) {
			log_error("protocol error: invalid open_device message");
			return -1;
		}

		res = handle_open_device(client, path);
		break;
	}
	case CLIENT_CLOSE_DEVICE: {
		struct proto_client_close_device msg;
		if (sizeof msg > size || connection_get(&client->connection, &msg, sizeof msg) == -1) {
			log_error("protocol error: invalid close_device message");
			return -1;
		}

		res = handle_close_device(client, msg.device_id);
		break;
	}
	case CLIENT_SWITCH_SESSION: {
		struct proto_client_switch_session msg;
		if (sizeof msg > size || connection_get(&client->connection, &msg, sizeof msg) == -1) {
			log_error("protocol error: invalid switch_session message");
			return -1;
		}

		res = handle_switch_session(client, msg.session);
		break;
	}
	case CLIENT_DISABLE_SEAT: {
		if (size != 0) {
			log_error("protocol error: invalid disable_seat message");
			return -1;
		}
		res = handle_disable_seat(client);
		break;
	}
	default:
		log_errorf("protocol error: unknown opcode: %d", opcode);
		res = -1;
		break;
	}
	if (res != -1) {
		res = client_flush(client);
	}
	return res;
}

int client_send_disable_seat(struct client *client) {
	struct proto_header header = {
		.opcode = SERVER_DISABLE_SEAT,
		.size = 0,
	};
	if (connection_put(&client->connection, &header, sizeof header) == -1 ||
	    connection_flush(&client->connection) == -1) {
		log_error("unable to send event");
		return -1;
	}
	return 0;
}

int client_send_enable_seat(struct client *client) {
	struct proto_header header = {
		.opcode = SERVER_ENABLE_SEAT,
		.size = 0,
	};
	if (connection_put(&client->connection, &header, sizeof header) == -1 ||
	    connection_flush(&client->connection) == -1) {
		log_error("unable to send event");
		return -1;
	}
	return 0;
}

int client_handle_connection(int fd, uint32_t mask, void *data) {
	(void)fd;

	struct client *client = data;
	if (mask & EVENT_ERROR) {
		log_error("connection error");
		goto fail;
	}
	if (mask & EVENT_HANGUP) {
		log_info("client disconnected");
		goto fail;
	}

	if (mask & EVENT_WRITABLE) {
		int len = connection_flush(&client->connection);
		if (len == -1 && errno != EAGAIN) {
			log_error("could not flush client connection");
			goto fail;
		} else if (len >= 0) {
			event_source_fd_update(client->event_source, EVENT_READABLE);
		}
	}

	if (mask & EVENT_READABLE) {
		int len = connection_read(&client->connection);
		if (len == 0 || (len == -1 && errno != EAGAIN)) {
			log_error("could not read client connection");
			goto fail;
		}

		struct proto_header header;
		while (connection_get(&client->connection, &header, sizeof header) != -1) {
			if (connection_pending(&client->connection) < header.size) {
				connection_restore(&client->connection, sizeof header);
				break;
			}
			if (client_handle_opcode(client, header.opcode, header.size) == -1) {
				goto fail;
			}
		}
	}

	return 0;

fail:
	client_destroy(client);
	return -1;
}