]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_attach.c
Merge pull request #2719 from ssup2/master
[mirror_lxc.git] / src / lxc / tools / lxc_attach.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2010
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <termios.h>
37 #include <unistd.h>
38
39 #include <lxc/lxccontainer.h>
40
41 #include "arguments.h"
42 #include "attach.h"
43 #include "caps.h"
44 #include "config.h"
45 #include "confile.h"
46 #include "log.h"
47 #include "utils.h"
48
49 lxc_log_define(lxc_attach, lxc);
50
51 static int my_parser(struct lxc_arguments *args, int c, char *arg);
52 static int add_to_simple_array(char ***array, ssize_t *capacity, char *value);
53 static bool stdfd_is_pty(void);
54 static int lxc_attach_create_log_file(const char *log_file);
55
56 static int elevated_privileges;
57 static signed long new_personality = -1;
58 static int namespace_flags = -1;
59 static int remount_sys_proc;
60 static lxc_attach_env_policy_t env_policy = LXC_ATTACH_KEEP_ENV;
61 static char **extra_env;
62 static ssize_t extra_env_size;
63 static char **extra_keep;
64 static ssize_t extra_keep_size;
65
66 static const struct option my_longopts[] = {
67 {"elevated-privileges", optional_argument, 0, 'e'},
68 {"arch", required_argument, 0, 'a'},
69 {"namespaces", required_argument, 0, 's'},
70 {"remount-sys-proc", no_argument, 0, 'R'},
71 /* TODO: decide upon short option names */
72 {"clear-env", no_argument, 0, 500},
73 {"keep-env", no_argument, 0, 501},
74 {"keep-var", required_argument, 0, 502},
75 {"set-var", required_argument, 0, 'v'},
76 {"pty-log", required_argument, 0, 'L'},
77 {"rcfile", required_argument, 0, 'f'},
78 {"uid", required_argument, 0, 'u'},
79 {"gid", required_argument, 0, 'g'},
80 LXC_COMMON_OPTIONS
81 };
82
83 static struct lxc_arguments my_args = {
84 .progname = "lxc-attach",
85 .help = "\
86 --name=NAME [-- COMMAND]\n\
87 \n\
88 Execute the specified COMMAND - enter the container NAME\n\
89 \n\
90 Options :\n\
91 -n, --name=NAME NAME of the container\n\
92 -e, --elevated-privileges=PRIVILEGES\n\
93 Use elevated privileges instead of those of the\n\
94 container. If you don't specify privileges to be\n\
95 elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
96 cgroup and restrictions, respectively) then all of them\n\
97 will be elevated.\n\
98 WARNING: This may leak privileges into the container.\n\
99 Use with care.\n\
100 -a, --arch=ARCH Use ARCH for program instead of container's own\n\
101 architecture.\n\
102 -s, --namespaces=FLAGS\n\
103 Don't attach to all the namespaces of the container\n\
104 but just to the following OR'd list of flags:\n\
105 MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
106 WARNING: Using -s implies -e with all privileges\n\
107 elevated, it may therefore leak privileges into the\n\
108 container. Use with care.\n\
109 -R, --remount-sys-proc\n\
110 Remount /sys and /proc if not attaching to the\n\
111 mount namespace when using -s in order to properly\n\
112 reflect the correct namespace context. See the\n\
113 lxc-attach(1) manual page for details.\n\
114 --clear-env Clear all environment variables before attaching.\n\
115 The attached shell/program will start with only\n\
116 container=lxc set.\n\
117 --keep-env Keep all current environment variables. This\n\
118 is the current default behaviour, but is likely to\n\
119 change in the future.\n\
120 -L, --pty-log=FILE\n\
121 Log pty output to FILE\n\
122 -v, --set-var Set an additional variable that is seen by the\n\
123 attached program in the container. May be specified\n\
124 multiple times.\n\
125 --keep-var Keep an additional environment variable. Only\n\
126 applicable if --clear-env is specified. May be used\n\
127 multiple times.\n\
128 -f, --rcfile=FILE\n\
129 Load configuration file FILE\n\
130 -u, --uid=UID Execute COMMAND with UID inside the container\n\
131 -g, --gid=GID Execute COMMAND with GID inside the container\n\
132 ",
133 .options = my_longopts,
134 .parser = my_parser,
135 .checker = NULL,
136 .log_priority = "ERROR",
137 .log_file = "none",
138 };
139
140 static int my_parser(struct lxc_arguments *args, int c, char *arg)
141 {
142 int ret;
143
144 switch (c) {
145 case 'e':
146 ret = lxc_fill_elevated_privileges(arg, &elevated_privileges);
147 if (ret)
148 return -1;
149 break;
150 case 'R': remount_sys_proc = 1; break;
151 case 'a':
152 new_personality = lxc_config_parse_arch(arg);
153 if (new_personality < 0) {
154 ERROR("Invalid architecture specified: %s", arg);
155 return -1;
156 }
157 break;
158 case 's':
159 namespace_flags = 0;
160
161 if (lxc_namespace_2_std_identifiers(arg) < 0)
162 return -1;
163
164 ret = lxc_fill_namespace_flags(arg, &namespace_flags);
165 if (ret)
166 return -1;
167
168 /* -s implies -e */
169 lxc_fill_elevated_privileges(NULL, &elevated_privileges);
170 break;
171 case 500: /* clear-env */
172 env_policy = LXC_ATTACH_CLEAR_ENV;
173 break;
174 case 501: /* keep-env */
175 env_policy = LXC_ATTACH_KEEP_ENV;
176 break;
177 case 502: /* keep-var */
178 ret = add_to_simple_array(&extra_keep, &extra_keep_size, arg);
179 if (ret < 0) {
180 ERROR("Failed to alloc memory");
181 return -1;
182 }
183 break;
184 case 'v':
185 ret = add_to_simple_array(&extra_env, &extra_env_size, arg);
186 if (ret < 0) {
187 ERROR("Failed to alloc memory");
188 return -1;
189 }
190 break;
191 case 'L':
192 args->console_log = arg;
193 break;
194 case 'f':
195 args->rcfile = arg;
196 break;
197 case 'u':
198 if (lxc_safe_uint(arg, &args->uid) < 0)
199 return -1;
200 break;
201 case 'g':
202 if (lxc_safe_uint(arg, &args->gid) < 0)
203 return -1;
204 break;
205 }
206
207 return 0;
208 }
209
210 static int add_to_simple_array(char ***array, ssize_t *capacity, char *value)
211 {
212 ssize_t count = 0;
213
214 if (!array)
215 return -1;
216
217 if (*array)
218 for (; (*array)[count]; count++);
219
220 /* we have to reallocate */
221 if (count >= *capacity - 1) {
222 ssize_t new_capacity = ((count + 1) / 32 + 1) * 32;
223
224 char **new_array = realloc((void*)*array, sizeof(char *) * new_capacity);
225 if (!new_array)
226 return -1;
227
228 memset(&new_array[count], 0, sizeof(char*)*(new_capacity - count));
229
230 *array = new_array;
231 *capacity = new_capacity;
232 }
233
234 if (!(*array))
235 return -1;
236
237 (*array)[count] = value;
238 return 0;
239 }
240
241 static bool stdfd_is_pty(void)
242 {
243 if (isatty(STDIN_FILENO))
244 return true;
245
246 if (isatty(STDOUT_FILENO))
247 return true;
248
249 if (isatty(STDERR_FILENO))
250 return true;
251
252 return false;
253 }
254
255 static int lxc_attach_create_log_file(const char *log_file)
256 {
257 int fd;
258
259 fd = open(log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600);
260 if (fd < 0) {
261 ERROR("Failed to open log file \"%s\"", log_file);
262 return -1;
263 }
264
265 return fd;
266 }
267
268 int main(int argc, char *argv[])
269 {
270 int ret = -1;
271 int wexit = 0;
272 struct lxc_log log;
273 pid_t pid;
274 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
275 lxc_attach_command_t command = (lxc_attach_command_t){.program = NULL};
276
277 if (lxc_caps_init())
278 exit(EXIT_FAILURE);
279
280 if (lxc_arguments_parse(&my_args, argc, argv))
281 exit(EXIT_FAILURE);
282
283 log.name = my_args.name;
284 log.file = my_args.log_file;
285 log.level = my_args.log_priority;
286 log.prefix = my_args.progname;
287 log.quiet = my_args.quiet;
288 log.lxcpath = my_args.lxcpath[0];
289
290 if (lxc_log_init(&log))
291 exit(EXIT_FAILURE);
292
293 if (geteuid())
294 if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
295 ERROR("You lack access to %s", my_args.lxcpath[0]);
296 exit(EXIT_FAILURE);
297 }
298
299 struct lxc_container *c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
300 if (!c)
301 exit(EXIT_FAILURE);
302
303 if (my_args.rcfile) {
304 c->clear_config(c);
305 if (!c->load_config(c, my_args.rcfile)) {
306 ERROR("Failed to load rcfile");
307 lxc_container_put(c);
308 exit(EXIT_FAILURE);
309 }
310
311 c->configfile = strdup(my_args.rcfile);
312 if (!c->configfile) {
313 ERROR("Out of memory setting new config filename");
314 lxc_container_put(c);
315 exit(EXIT_FAILURE);
316 }
317 }
318
319 if (!c->may_control(c)) {
320 ERROR("Insufficent privileges to control %s", c->name);
321 lxc_container_put(c);
322 exit(EXIT_FAILURE);
323 }
324
325 if (remount_sys_proc)
326 attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS;
327
328 if (elevated_privileges)
329 attach_options.attach_flags &= ~(elevated_privileges);
330
331 if (stdfd_is_pty())
332 attach_options.attach_flags |= LXC_ATTACH_TERMINAL;
333
334 attach_options.namespaces = namespace_flags;
335 attach_options.personality = new_personality;
336 attach_options.env_policy = env_policy;
337 attach_options.extra_env_vars = extra_env;
338 attach_options.extra_keep_env = extra_keep;
339
340 if (my_args.argc > 0) {
341 command.program = my_args.argv[0];
342 command.argv = (char**)my_args.argv;
343 }
344
345 if (my_args.console_log) {
346 attach_options.log_fd = lxc_attach_create_log_file(my_args.console_log);
347 if (attach_options.log_fd < 0)
348 goto out;
349 }
350
351 if (my_args.uid)
352 attach_options.uid = my_args.uid;
353
354 if (my_args.gid)
355 attach_options.gid = my_args.gid;
356
357 if (command.program)
358 ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid);
359 else
360 ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
361 if (ret < 0)
362 goto out;
363
364 ret = lxc_wait_for_pid_status(pid);
365 if (ret < 0)
366 goto out;
367
368 if (WIFEXITED(ret))
369 wexit = WEXITSTATUS(ret);
370
371 out:
372 lxc_container_put(c);
373 if (ret >= 0)
374 exit(wexit);
375
376 exit(EXIT_FAILURE);
377 }