jail: handle containers seperately
[oweals/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         /* We use the same settings that ssh does. */
42         newtios.c_iflag |= IGNPAR;
43         newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
44         newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
45         newtios.c_oflag &= ~ONLCR;
46         newtios.c_oflag |= OPOST;
47         newtios.c_cc[VMIN] = 1;
48         newtios.c_cc[VTIME] = 0;
49
50         /* Set new attributes */
51         if (tcsetattr(fd, TCSAFLUSH, &newtios))
52                 return -1;
53
54         return 0;
55 }
56
57
58
59 #define OPT_ARGS        "i:c:"
60
61 static struct ustream_fd cufd;
62 static struct ustream_fd lufd;
63
64 static void usage()
65 {
66         fprintf(stderr, "ujail-console -c <container> [-i <instance>]\n");
67         exit(1);
68 }
69
70 static void client_cb(struct ustream *s, int bytes)
71 {
72         char *buf;
73         int len, rv;
74
75         do {
76                 buf = ustream_get_read_buf(s, &len);
77                 if (!buf)
78                         break;
79
80                 rv = ustream_write(&lufd.stream, buf, len, false);
81
82                 if (rv > 0)
83                         ustream_consume(s, rv);
84
85                 if (rv <= len)
86                         break;
87         } while(1);
88 }
89
90 static void local_cb(struct ustream *s, int bytes)
91 {
92         char *buf;
93         int len, rv;
94
95         do {
96                 buf = ustream_get_read_buf(s, &len);
97                 if (!buf)
98                         break;
99
100                 if ((len > 0) && (buf[0] == 2))
101                                 uloop_end();
102
103                 rv = ustream_write(&cufd.stream, buf, len, false);
104
105                 if (rv > 0)
106                         ustream_consume(s, rv);
107
108                 if (rv <= len)
109                         break;
110         } while(1);
111 }
112
113 int main(int argc, char **argv)
114 {
115         struct ubus_context *ctx;
116         uint32_t id;
117         static struct blob_buf req;
118         char *container_name = NULL, *instance_name = NULL;
119         int client_fd, server_fd, tty_fd;
120         struct termios oldtermios;
121         int ch;
122
123         while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
124                 switch (ch) {
125                 case 'i':
126                         instance_name = optarg;
127                         break;
128                 case 'c':
129                         container_name = optarg;
130                         break;
131                 default:
132                         usage();
133                 }
134         }
135
136         if (!container_name)
137                 usage();
138
139         ctx = ubus_connect(NULL);
140         if (!ctx) {
141                 fprintf(stderr, "can't connect to ubus!\n");
142                 return -1;
143         }
144
145         /* open pseudo-terminal pair */
146         client_fd = posix_openpt(O_RDWR | O_NOCTTY);
147         if (client_fd < 0) {
148                 fprintf(stderr, "can't create virtual console!\n");
149                 ubus_free(ctx);
150                 return -1;
151         }
152         setup_tios(client_fd, &oldtermios);
153         grantpt(client_fd);
154         unlockpt(client_fd);
155         server_fd = open(ptsname(client_fd), O_RDWR | O_NOCTTY);
156         if (server_fd < 0) {
157                 fprintf(stderr, "can't open virtual console!\n");
158                 close(client_fd);
159                 ubus_free(ctx);
160                 return -1;
161         }
162
163         setup_tios(server_fd, &oldtermios);
164         tty_fd = open("/dev/tty", O_RDWR);
165         setup_tios(tty_fd, &oldtermios);
166
167         /* register server-side with procd */
168         blob_buf_init(&req, 0);
169         blobmsg_add_string(&req, "name", container_name);
170         if (instance_name)
171                 blobmsg_add_string(&req, "instance", instance_name);
172
173         if (ubus_lookup_id(ctx, "container", &id) ||
174             ubus_invoke_fd(ctx, id, "console_attach", req.head, NULL, NULL, 3000, server_fd)) {
175                 fprintf(stderr, "ubus request failed\n");
176                 close(server_fd);
177                 close(client_fd);
178                 blob_buf_free(&req);
179                 ubus_free(ctx);
180                 return -2;
181         }
182
183         close(server_fd);
184         blob_buf_free(&req);
185         ubus_free(ctx);
186
187         uloop_init();
188
189         /* forward between stdio and client_fd until detach is requested */
190         lufd.stream.notify_read = local_cb;
191         ustream_fd_init(&lufd, tty_fd);
192
193         cufd.stream.notify_read = client_cb;
194 /* ToDo: handle remote close and other events */
195 //      cufd.stream.notify_state = client_state_cb;
196         ustream_fd_init(&cufd, client_fd);
197
198         fprintf(stderr, "attaching to jail console. press [CTRL]+[B] to exit.\n");
199         close(0);
200         close(1);
201         close(2);
202         uloop_run();
203
204         tcsetattr(tty_fd, TCSAFLUSH, &oldtermios);
205         ustream_free(&lufd.stream);
206         ustream_free(&cufd.stream);
207         close(client_fd);
208
209         return 0;
210 }