blob: cb69968a35c02de45f68044261bd8a01bc6605c8 [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#define _GNU_SOURCE
24
25#include "config.h"
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <assert.h>
31#include <errno.h>
32
33#include <error.h>
34#include <getopt.h>
35
36#include <sys/types.h>
37#include <sys/ioctl.h>
38#include <sys/stat.h>
39#include <sys/wait.h>
40#include <sys/socket.h>
41#include <sys/epoll.h>
42#include <sys/signalfd.h>
43#include <signal.h>
44#include <unistd.h>
45#include <fcntl.h>
46
47#include <termios.h>
48#include <linux/vt.h>
49#include <linux/major.h>
50
51#include <pwd.h>
52#include <grp.h>
53#include <security/pam_appl.h>
54
55#include <xf86drm.h>
56
57#ifdef HAVE_SYSTEMD_LOGIN
58#include <systemd/sd-login.h>
59#endif
60
61#include "weston-launch.h"
62
63struct weston_launch {
64 struct pam_conv pc;
65 pam_handle_t *ph;
66 int tty;
67 int ttynr;
68 int sock[2];
69 struct passwd *pw;
70
71 int epollfd;
72 int signalfd;
73
74 pid_t child;
75 int verbose;
76};
77
Kristian Høgsberg9e140912012-04-10 01:26:18 -040078union cmsg_data { unsigned char b[4]; int fd; };
79
Benjamin Franzkebfeda132012-01-30 14:04:04 +010080static gid_t *
81read_groups(void)
82{
83 int n;
84 gid_t *groups;
85
86 n = getgroups(0, NULL);
87 groups = malloc(n * sizeof(gid_t));
88 if (!groups)
89 return NULL;
90
91 if (getgroups(n, groups) < 0) {
92 free(groups);
93 return NULL;
94 }
95 return groups;
96}
97
98static int
99weston_launch_allowed(struct weston_launch *wl)
100{
101 struct group *gr;
102 gid_t *groups;
103 int i;
104#ifdef HAVE_SYSTEMD_LOGIN
105 char *session, *seat;
106 int err;
107#endif
108
109 if (getuid() == 0)
110 return 1;
111
112 gr = getgrnam("weston-launch");
113 if (gr) {
114 groups = read_groups();
115 if (groups) {
116 for (i = 0; groups[i]; ++i) {
117 if (groups[i] == gr->gr_gid) {
118 free(groups);
119 return 1;
120 }
121 }
122 free(groups);
123 }
124 }
125
126#ifdef HAVE_SYSTEMD_LOGIN
127 err = sd_pid_get_session(getpid(), &session);
128 if (err == 0 && session) {
129 if (sd_session_is_active(session) &&
130 sd_session_get_seat(session, &seat) == 0) {
131 free(seat);
132 free(session);
133 return 1;
134 }
135 free(session);
136 }
137#endif
138
139 return 0;
140}
141
142static int
143pam_conversation_fn(int msg_count,
144 const struct pam_message **messages,
145 struct pam_response **responses,
146 void *user_data)
147{
148 return PAM_SUCCESS;
149}
150
151static int
152setup_pam(struct weston_launch *wl)
153{
154 int err;
155
156 wl->pc.conv = pam_conversation_fn;
157 wl->pc.appdata_ptr = wl;
158
159 err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
John Kåre Alsaker5b90d8f2012-10-12 12:25:05 +0200160 if (err != PAM_SUCCESS) {
161 fprintf(stderr, "failed to start pam transaction: %d: %s\n",
162 err, pam_strerror(wl->ph, err));
163 return -1;
164 }
165
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100166 err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
167 if (err != PAM_SUCCESS) {
168 fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
169 err, pam_strerror(wl->ph, err));
170 return -1;
171 }
172
173 err = pam_open_session(wl->ph, 0);
174 if (err != PAM_SUCCESS) {
175 fprintf(stderr, "failed to open pam session: %d: %s\n",
176 err, pam_strerror(wl->ph, err));
177 return -1;
178 }
179
180 return 0;
181}
182
183static int
184setup_launcher_socket(struct weston_launch *wl)
185{
186 struct epoll_event ev;
187
188 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, wl->sock) < 0)
189 error(1, errno, "socketpair failed");
190
191 fcntl(wl->sock[0], F_SETFD, O_CLOEXEC);
192
193 memset(&ev, 0, sizeof ev);
194 ev.events = EPOLLIN;
195 ev.data.fd = wl->sock[0];
196 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
197 return -errno;
198
199 return 0;
200}
201
202static int
203setup_signals(struct weston_launch *wl)
204{
205 sigset_t mask;
206 struct sigaction sa;
207 struct epoll_event ev;
208
209 memset(&sa, 0, sizeof sa);
210 sa.sa_handler = SIG_DFL;
211 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
212 assert(sigaction(SIGCHLD, &sa, NULL) == 0);
213
214 assert(sigemptyset(&mask) == 0);
215 sigaddset(&mask, SIGCHLD);
216 sigaddset(&mask, SIGINT);
217 sigaddset(&mask, SIGTERM);
218 assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
219
220 wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
221 if (wl->signalfd < 0)
222 return -errno;
223
224 memset(&ev, 0, sizeof ev);
225 ev.events = EPOLLIN;
226 ev.data.fd = wl->signalfd;
227 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
228 return -errno;
229
230 return 0;
231}
232
233static void
234setenv_fd(const char *env, int fd)
235{
236 char buf[32];
237
238 snprintf(buf, sizeof buf, "%d", fd);
239 setenv(env, buf, 1);
240}
241
242static int
243handle_setmaster(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
244{
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400245 int ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100246 struct cmsghdr *cmsg;
247 struct weston_launcher_set_master *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400248 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100249
250 if (len != sizeof(*message)) {
251 error(0, 0, "missing value in setmaster request");
252 goto out;
253 }
254
255 message = msg->msg_iov->iov_base;
256
257 cmsg = CMSG_FIRSTHDR(msg);
258 if (!cmsg ||
259 cmsg->cmsg_level != SOL_SOCKET ||
260 cmsg->cmsg_type != SCM_RIGHTS) {
261 error(0, 0, "invalid control message");
262 goto out;
263 }
264
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400265 data = (union cmsg_data *) CMSG_DATA(cmsg);
266 if (data->fd == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100267 error(0, 0, "missing drm fd in socket request");
268 goto out;
269 }
270
271 if (message->set_master)
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400272 ret = drmSetMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100273 else
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400274 ret = drmDropMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100275
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400276 close(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100277out:
278 do {
279 len = send(wl->sock[0], &ret, sizeof ret, 0);
280 } while (len < 0 && errno == EINTR);
281 if (len < 0)
282 return -1;
283
284 return 0;
285}
286
287static int
288handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
289{
290 int fd = -1, ret = -1;
291 char control[CMSG_SPACE(sizeof(fd))];
292 struct cmsghdr *cmsg;
293 struct stat s;
294 struct msghdr nmsg;
295 struct iovec iov;
296 struct weston_launcher_open *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400297 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100298
299 message = msg->msg_iov->iov_base;
300 if ((size_t)len < sizeof(*message))
301 goto err0;
302
303 /* Ensure path is null-terminated */
304 ((char *) message)[len-1] = '\0';
305
306 if (stat(message->path, &s) < 0)
307 goto err0;
308
309 fd = open(message->path, message->flags);
310 if (fd < 0)
311 goto err0;
312
313 if (major(s.st_rdev) != INPUT_MAJOR) {
314 close(fd);
315 fd = -1;
316 goto err0;
317 }
318
319err0:
320 memset(&nmsg, 0, sizeof nmsg);
321 nmsg.msg_iov = &iov;
322 nmsg.msg_iovlen = 1;
323 if (fd != -1) {
324 nmsg.msg_control = control;
325 nmsg.msg_controllen = sizeof control;
326 cmsg = CMSG_FIRSTHDR(&nmsg);
327 cmsg->cmsg_level = SOL_SOCKET;
328 cmsg->cmsg_type = SCM_RIGHTS;
329 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400330 data = (union cmsg_data *) CMSG_DATA(cmsg);
331 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100332 nmsg.msg_controllen = cmsg->cmsg_len;
333 ret = 0;
334 }
335 iov.iov_base = &ret;
336 iov.iov_len = sizeof ret;
337
338 if (wl->verbose)
339 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
340 message->path, ret, fd);
341 do {
342 len = sendmsg(wl->sock[0], &nmsg, 0);
343 } while (len < 0 && errno == EINTR);
344
345 if (len < 0)
346 return -1;
347
348 return 0;
349}
350
351static int
352handle_socket_msg(struct weston_launch *wl)
353{
354 char control[CMSG_SPACE(sizeof(int))];
355 char buf[BUFSIZ];
356 struct msghdr msg;
357 struct iovec iov;
358 int ret = -1;
359 ssize_t len;
360 struct weston_launcher_message *message;
361
362 memset(&msg, 0, sizeof(msg));
363 iov.iov_base = buf;
364 iov.iov_len = sizeof buf;
365 msg.msg_iov = &iov;
366 msg.msg_iovlen = 1;
367 msg.msg_control = control;
368 msg.msg_controllen = sizeof control;
369
370 do {
371 len = recvmsg(wl->sock[0], &msg, 0);
372 } while (len < 0 && errno == EINTR);
373
374 if (len < 1)
375 return -1;
376
377 message = (void *) buf;
378 switch (message->opcode) {
379 case WESTON_LAUNCHER_OPEN:
380 ret = handle_open(wl, &msg, len);
381 break;
382 case WESTON_LAUNCHER_DRM_SET_MASTER:
383 ret = handle_setmaster(wl, &msg, len);
384 break;
385 }
386
387 return ret;
388}
389
390static void
391quit(struct weston_launch *wl, int status)
392{
393 int err;
394
395 close(wl->epollfd);
396 close(wl->signalfd);
397 close(wl->sock[0]);
398
399 err = pam_close_session(wl->ph, 0);
400 if (err)
401 fprintf(stderr, "pam_close_session failed: %d: %s\n",
402 err, pam_strerror(wl->ph, err));
403 pam_end(wl->ph, err);
404
405 exit(status);
406}
407
408static int
409handle_signal(struct weston_launch *wl)
410{
411 struct signalfd_siginfo sig;
412 int pid, status;
413
414 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
415 error(0, errno, "reading signalfd failed");
416 return -1;
417 }
418
419 switch (sig.ssi_signo) {
420 case SIGCHLD:
421 pid = waitpid(-1, &status, 0);
422 if (pid == wl->child) {
423 wl->child = 0;
424 quit(wl, WIFEXITED(status) ? WEXITSTATUS(status) : 0);
425 }
426 break;
427 case SIGTERM:
428 if (wl->child)
429 kill(wl->child, SIGTERM);
430 quit(wl, 0);
431 break;
432 case SIGINT:
433 if (wl->child)
434 kill(wl->child, SIGTERM);
435 break;
436 default:
437 return -1;
438 }
439
440 return 0;
441}
442
443static int
444setup_tty(struct weston_launch *wl, const char *tty)
445{
446 struct stat buf;
447 char *t;
448
449 if (tty) {
450 t = ttyname(STDIN_FILENO);
451 if (t && strcmp(t, tty) == 0)
452 wl->tty = STDIN_FILENO;
453 else
454 wl->tty = open(tty, O_RDWR | O_NOCTTY);
455 } else {
456 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
457 char filename[16];
458
459 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300460 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100461
462 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
463 error(1, errno, "failed to find non-opened console");
464
465 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
466 wl->tty = open(filename, O_RDWR | O_NOCTTY);
467 close(tty0);
468 }
469
470 if (wl->tty < 0)
471 error(1, errno, "failed to open tty");
472
473 if (tty) {
474 if (fstat(wl->tty, &buf) < 0)
475 error(1, errno, "stat %s failed", tty);
476
477 if (major(buf.st_rdev) != TTY_MAJOR)
478 error(1, 0, "invalid tty device: %s", tty);
479
480 wl->ttynr = minor(buf.st_rdev);
481 }
482
483 return 0;
484}
485
486static void
487help(const char *name)
488{
489 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
490 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300491 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100492 fprintf(stderr, " -v, --verbose Be verbose\n");
493 fprintf(stderr, " -s, --sleep Sleep specified amount of time before exec\n");
494 fprintf(stderr, " -h, --help Display this help message\n");
495}
496
497int
498main(int argc, char *argv[])
499{
500 struct weston_launch wl;
501 char **env;
502 int i, c;
503 char **child_argv;
504 char *tty = NULL, *new_user = NULL;
505 int sleep_fork = 0;
506 struct option opts[] = {
507 { "user", required_argument, NULL, 'u' },
508 { "tty", required_argument, NULL, 't' },
509 { "verbose", no_argument, NULL, 'v' },
510 { "sleep", optional_argument, NULL, 's' },
511 { "help", no_argument, NULL, 'h' },
512 { 0, 0, NULL, 0 }
513 };
514
515 memset(&wl, 0, sizeof wl);
516
517 while ((c = getopt_long(argc, argv, "u:t:s::vh", opts, &i)) != -1) {
518 switch (c) {
519 case 'u':
520 new_user = optarg;
521 if (getuid() != 0)
522 error(1, 0, "Permission denied. -u allowed for root only");
523 break;
524 case 't':
525 tty = optarg;
526 break;
527 case 'v':
528 wl.verbose = 1;
529 break;
530 case 's':
531 if (optarg)
532 sleep_fork = atoi(optarg);
533 else
534 sleep_fork = 10;
535 break;
536 case 'h':
537 help("weston");
538 exit(1);
539 }
540 }
541
542 child_argv = &argv[optind-1];
543 child_argv[0] = BINDIR "/weston";
544
545 if (new_user)
546 wl.pw = getpwnam(new_user);
547 else
548 wl.pw = getpwuid(getuid());
549 if (wl.pw == NULL)
550 error(1, errno, "failed to get username");
551
552 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300553 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100554#ifdef HAVE_SYSTEMD_LOGIN
555 " - run from an active and local (systemd) session.\n"
556#else
557 " - enable systemd session support for weston-launch.\n"
558#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300559 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100560
561 if (setup_tty(&wl, tty) < 0)
562 return 1;
563
564 if (setup_pam(&wl) < 0)
565 return 1;
566
567 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
568 if (wl.epollfd < 0)
569 error(1, errno, "epoll create failed");
570
571 if (setup_launcher_socket(&wl) < 0)
572 return 1;
573
574 if (setup_signals(&wl) < 0)
575 return 1;
576
577 switch ((wl.child = fork())) {
578 case -1:
579 error(1, errno, "fork failed");
580 break;
581 case 0:
582 if (wl.verbose)
583 printf("weston-launch: spawned weston with pid: %d\n", getpid());
584 if (wl.tty != STDIN_FILENO) {
585 if (setsid() < 0)
586 error(1, errno, "setsid failed");
587 if (ioctl(wl.tty, TIOCSCTTY, 0) < 0)
588 error(1, errno, "TIOCSCTTY failed - tty is in use");
589 }
590
591 if (setgid(wl.pw->pw_gid) < 0 ||
592 setuid(wl.pw->pw_uid) < 0)
593 error(1, errno, "dropping privilidges failed");
594
595 if (sleep_fork) {
596 if (wl.verbose)
597 printf("weston-launch: waiting %d seconds\n", sleep_fork);
598 sleep(sleep_fork);
599 }
600
601 if (new_user) {
602 setenv("USER", wl.pw->pw_name, 1);
603 setenv("LOGNAME", wl.pw->pw_name, 1);
604 setenv("HOME", wl.pw->pw_dir, 1);
605 setenv("SHELL", wl.pw->pw_shell, 1);
606 }
607 env = pam_getenvlist(wl.ph);
608 if (env) {
609 for (i = 0; env[i]; ++i) {
610 if (putenv(env[i]) < 0)
611 error(0, 0, "putenv %s failed", env[i]);
612 }
613 free(env);
614 }
615
616 if (wl.tty != STDIN_FILENO)
617 setenv_fd("WESTON_TTY_FD", wl.tty);
618
619 setenv_fd("WESTON_LAUNCHER_SOCK", wl.sock[1]);
620
621 unsetenv("DISPLAY");
622
623 execv(child_argv[0], child_argv);
624 error(1, errno, "exec failed");
625 break;
626 default:
627 close(wl.sock[1]);
628 if (wl.tty != STDIN_FILENO)
629 close(wl.tty);
630
631 while (1) {
632 struct epoll_event ev;
633 int n;
634
635 n = epoll_wait(wl.epollfd, &ev, 1, -1);
636 if (n < 0)
637 error(0, errno, "epoll_wait failed");
638 if (n != 1)
639 continue;
640
641 if (ev.data.fd == wl.sock[0])
642 handle_socket_msg(&wl);
643 else if (ev.data.fd == wl.signalfd)
644 handle_signal(&wl);
645 }
646 break;
647 }
648
649 return 0;
650}