jail: add option to provide /dev/console to containers
[project/procd.git] / jail / console.c
1 /*
2 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <libubox/ustream.h>
17 #include <libubus.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <termios.h>
26
27 static inline int setup_tios(int fd, struct termios *oldtios)
28 {
29 struct termios newtios;
30
31 if (!isatty(fd)) {
32 return -1;
33 }
34
35 /* Get current termios */
36 if (tcgetattr(fd, oldtios))
37 return -1;
38
39 newtios = *oldtios;
40
41 /* Remove the echo characters and signal reception, the echo
42 * will be done with master proxying */
43 newtios.c_iflag &= ~IGNBRK;
44 newtios.c_iflag &= BRKINT;
45 newtios.c_lflag &= ~(ECHO|ICANON|ISIG);
46 newtios.c_cc[VMIN] = 1;
47 newtios.c_cc[VTIME] = 0;
48
49 /* Set new attributes */
50 if (tcsetattr(fd, TCSAFLUSH, &newtios))
51 return -1;
52
53 return 0;
54 }
55
56
57
58 #define OPT_ARGS "i:s:"
59
60 static struct ustream_fd cufd;
61 static struct ustream_fd lufd;
62
63 static void usage()
64 {
65 fprintf(stderr, "ujail-console -s <service> [-i <instance>]\n");
66 exit(1);
67 }
68
69 static void client_cb(struct ustream *s, int bytes)
70 {
71 char *buf;
72 int len, rv;
73
74 do {
75 buf = ustream_get_read_buf(s, &len);
76 if (!buf)
77 break;
78
79 rv = ustream_write(&lufd.stream, buf, len, false);
80
81 if (rv > 0)
82 ustream_consume(s, rv);
83
84 if (rv <= len)
85 break;
86 } while(1);
87 }
88
89 static void local_cb(struct ustream *s, int bytes)
90 {
91 char *buf;
92 int len, rv;
93
94 do {
95 buf = ustream_get_read_buf(s, &len);
96 if (!buf)
97 break;
98
99 if ((len > 0) && (buf[0] == 2))
100 uloop_end();
101
102 rv = ustream_write(&cufd.stream, buf, len, false);
103
104 if (rv > 0)
105 ustream_consume(s, rv);
106
107 if (rv <= len)
108 break;
109 } while(1);
110 }
111
112 int main(int argc, char **argv)
113 {
114 struct ubus_context *ctx;
115 uint32_t id;
116 static struct blob_buf req;
117 char *service_name = NULL, *instance_name = NULL;
118 int client_fd, server_fd, tty_fd;
119 struct termios oldtermios;
120 int ch;
121
122 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
123 switch (ch) {
124 case 'i':
125 instance_name = optarg;
126 break;
127 case 's':
128 service_name = optarg;
129 break;
130 default:
131 usage();
132 }
133 }
134
135 if (!service_name)
136 usage();
137
138 ctx = ubus_connect(NULL);
139 if (!ctx) {
140 fprintf(stderr, "can't connect to ubus!\n");
141 return -1;
142 }
143
144 /* open pseudo-terminal pair */
145 client_fd = posix_openpt(O_RDWR | O_NOCTTY);
146 if (client_fd < 0) {
147 fprintf(stderr, "can't create virtual console!\n");
148 ubus_free(ctx);
149 return -1;
150 }
151 setup_tios(client_fd, &oldtermios);
152 grantpt(client_fd);
153 unlockpt(client_fd);
154 server_fd = open(ptsname(client_fd), O_RDWR | O_NOCTTY);
155 if (server_fd < 0) {
156 fprintf(stderr, "can't open virtual console!\n");
157 close(client_fd);
158 ubus_free(ctx);
159 return -1;
160 }
161
162 setup_tios(server_fd, &oldtermios);
163 tty_fd = open("/dev/tty", O_RDWR);
164 setup_tios(tty_fd, &oldtermios);
165
166 /* register server-side with procd */
167 blob_buf_init(&req, 0);
168 blobmsg_add_string(&req, "name", service_name);
169 if (instance_name)
170 blobmsg_add_string(&req, "instance", instance_name);
171
172 if (ubus_lookup_id(ctx, "service", &id) ||
173 ubus_invoke_fd(ctx, id, "console_attach", req.head, NULL, NULL, 3000, server_fd)) {
174 fprintf(stderr, "ubus request failed\n");
175 close(server_fd);
176 close(client_fd);
177 blob_buf_free(&req);
178 ubus_free(ctx);
179 return -2;
180 }
181
182 close(server_fd);
183 blob_buf_free(&req);
184 ubus_free(ctx);
185
186 uloop_init();
187
188 /* forward between stdio and client_fd until detach is requested */
189 lufd.stream.notify_read = local_cb;
190 ustream_fd_init(&lufd, tty_fd);
191
192 cufd.stream.notify_read = client_cb;
193 /* ToDo: handle remote close and other events */
194 // cufd.stream.notify_state = client_state_cb;
195 ustream_fd_init(&cufd, client_fd);
196
197 fprintf(stderr, "attaching to jail console. press [CTRL]+[B] to exit.\n");
198 close(0);
199 close(1);
200 close(2);
201 uloop_run();
202
203 tcsetattr(tty_fd, TCSAFLUSH, &oldtermios);
204 ustream_free(&lufd.stream);
205 ustream_free(&cufd.stream);
206 close(client_fd);
207
208 return 0;
209 }