-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdocker-enter.c
More file actions
247 lines (199 loc) · 4.67 KB
/
docker-enter.c
File metadata and controls
247 lines (199 loc) · 4.67 KB
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
/*
* Author: Johan Hanssen Seferidis
*
* docker-enter
*
* */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sched.h>
#include <dirent.h>
#include <getopt.h>
#define USERNAME_MAX 32+1
#define CONT_ID_MAX 12+1
#define VERSION "docker-enter v0.1"
#define DEFAULT_SHELL "/bin/sh"
static char dir [PATH_MAX] = {'\0'}; // Working directory
static char user[USERNAME_MAX] = {'\0'}; // User
static char cont_ID[CONT_ID_MAX] = {'\0'}; // Container name
static pid_t cont_pid = -1; // Container PID
// Table with namespaces info
static struct namespace_info {
const char *name;
const char *file;
int type;
} namespace_table[] = {
{ "user", "ns/user", CLONE_NEWUSER },
{ "ipc", "ns/ipc", CLONE_NEWIPC },
{ "uts", "ns/uts", CLONE_NEWUTS },
{ "net", "ns/net", CLONE_NEWNET },
{ "pid", "ns/pid", CLONE_NEWPID },
{ "mnt", "ns/mnt", CLONE_NEWNS },
{ NULL, NULL, -1 }
};
/*
* Execute shell
*
* */
static void exec_shell()
{
const char *shell = getenv("SHELL");
if (!shell)
shell = DEFAULT_SHELL;
execl(shell, shell, NULL);
}
/*
* Translates a container ID to a PID (uses docker)
*
* */
static pid_t container_pid(char* cont_ID) {
pid_t cont_pid = -1;
char output[4096];
int pip[2];
pid_t pid;
if (pipe(pip)==-1)
perror("Pipe");
if ((pid = fork()) == -1)
perror("Fork");
if(pid == 0) { // child
dup2 (pip[1], STDOUT_FILENO);
close(pip[0]);
execlp( "docker", "docker", "inspect", "--format", "{{.State.Pid}}", cont_ID, (char*)0 );
exit(1);
} else { // parent
int status=-10;
wait(&status);
if (status != 0){
fprintf(stderr, "Docker terminated with error code %d\n", status);
exit(1);
}
close(pip[1]);
read(pip[0], output, sizeof(output));
sscanf(output, "%ld", (long int*)&cont_pid);
}
return cont_pid;
}
/*
* Prints on screen the usage of command
*
* */
static void usage_print()
{
printf("Usage: %s [options] <container ID>\n", program_invocation_short_name);
puts(" -h, --help display this help and exit");
puts(" -v, --version show version");
puts(" -u, --user=<user> login as user <user>");
puts(" -d, --directory=<dir> change directory to <directory>");
}
/*
* Parses the command given by the user.
* In practice it changes global variables.
*
* */
static void parse_command(int argc, char *argv[]){
int opt = 0;
static const struct option long_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "user", required_argument, 0, 'u' },
{ "directory", required_argument, 0, 'd' },
{ 0, 0, 0, 0 }
};
// Parse flags
while ((opt = getopt_long(argc, argv, "vhu:d:", long_opts, NULL)) != -1)
{
switch (opt)
{
case 'h':
usage_print();
exit(0);
case 'v':
puts(VERSION);
exit(0);
case 'u':
if (optarg) {
if (strlen(optarg) < 1) {
fprintf(stderr, "A user must be specified\n");
exit(1);
}
strcpy(user, optarg);
}
break;
case 'd':
if (optarg) {
if (strlen(optarg) < 1) {
fprintf(stderr, "A directory must be specified\n");
exit(1);
}
strcpy(dir, optarg);
}
break;
default:
usage_print();
exit(1);
}
}
// Parse container ID
strcpy(cont_ID, argv[argc-1]);
if ( argc==1 || strcmp(cont_ID, dir)==0 || strcmp(cont_ID, user)==0)
{
fprintf(stderr, "No container ID given. Use -h for help\n");
exit(1);
}
cont_pid = container_pid(cont_ID);
if ( cont_pid < 0) {
exit(1);
}
}
/*
* Opens a specified namespace
*
* */
static void open_namespace(char* ns_name) {
struct namespace_info *ns = namespace_table;
while ( ns->name != NULL ) {
if (ns->name == ns_name)
{
char pathbuf[PATH_MAX];
int fd = -1;
snprintf(pathbuf, sizeof(pathbuf), "/proc/%u/%s", cont_pid, ns->file);
if ((fd = open(pathbuf, O_RDONLY)) == -1) {
fprintf(stderr, "Trying to open namespace '%s': %s\n", ns->file, strerror(errno));
exit(1);
}
if (setns(fd, 0) < 0) {
fprintf(stderr, "Trying to set namespace '%s': %s\n", ns->file, strerror(errno));
exit(1);
}
return;
}
ns++;
}
exit(1);
}
int main (int argc, char *argv[]) {
// Parse command
parse_command(argc, argv);
// Open namespaces
//open_namespace("user");
open_namespace("ipc");
open_namespace("uts");
open_namespace("net");
open_namespace("pid");
open_namespace("mnt");
// Change directory
chdir(dir);
// Change user
if (strlen(user)>0)
execlp( "su", "su", user, (char*)0 );
// Run shell
exec_shell();
return 0;
}