0
|
1
|
|
2
|
|
3 #include <stdlib.h>
|
|
4 #include <unistd.h>
|
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <sys/types.h>
|
|
8 #include <unistd.h>
|
|
9 #include <signal.h>
|
|
10 #include <string.h>
|
|
11
|
|
12 #include <sys/types.h>
|
|
13 #include <regex.h>
|
|
14
|
|
15 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
|
|
16
|
1
|
17 #define command "/usr/bin/docker"
|
|
18 #define ps_command "/usr/bin/docker ps -a"
|
0
|
19 #define run_command "run"
|
1
|
20 #define build_command "build"
|
|
21 #define attach_command "attach"
|
|
22 #define dettach_command "dettach"
|
|
23 #define pull_command "pull" /* download docker image command */
|
|
24 #define images_command "images" /* list images command */
|
|
25 #define commit_command "commit" /* make image command */
|
|
26 #define rm_command "rm" /* remove container command */
|
|
27 #define rmi_command "rmi" /* remove image command */
|
|
28 #define start_command "start"
|
|
29 #define stop_command "stop"
|
0
|
30
|
|
31 static char bad_name[] = "Bad process name. Try students/e11/e115711/01 or teachers/kono/02\n";
|
|
32
|
|
33 #define PSNAME_MAX (512)
|
|
34
|
1
|
35 typedef struct pslist {
|
0
|
36 char name[PSNAME_MAX];
|
|
37 struct pslist *next;
|
|
38 } PSLIST, *PSLISTPTR;
|
|
39
|
|
40 #define NEW(type) ((type*)malloc(sizeof(type)))
|
|
41
|
|
42 /* Define global variables */
|
|
43
|
|
44 PSLISTPTR
|
|
45 get_pslist(regex_t *list_pattern)
|
|
46 {
|
|
47 PSLISTPTR list = NEW(PSLIST);
|
1
|
48 PSLISTPTR p = list;
|
0
|
49 p->name[0] = 0;
|
|
50 p->next = 0;
|
1
|
51 FILE *fp = popen(ps_command,"r");
|
0
|
52 while(fgets(p->name,PSNAME_MAX,fp)!=NULL) {
|
|
53 if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue;
|
|
54 p->next = NEW(PSLIST);
|
|
55 p = p->next;
|
|
56 }
|
|
57 p->name[0] = 0;
|
|
58 pclose(fp);
|
|
59 return list;
|
|
60 }
|
|
61
|
|
62 void
|
|
63 print_pslist(PSLISTPTR list)
|
|
64 {
|
1
|
65 for(;list && list->name[0]; list = list->next) {
|
0
|
66 fprintf(stdout, " %s\n",list->name);
|
|
67 }
|
|
68 }
|
|
69
|
|
70 int
|
|
71 check_pslist_name(PSLISTPTR list, char *arg)
|
|
72 {
|
1
|
73 for(;list && list->name[0]; list = list->next) {
|
0
|
74 if (strstr(list->name,arg)!=0) return 1;
|
|
75 }
|
|
76 return 0;
|
|
77 }
|
|
78
|
|
79 int
|
|
80 check_name(const char *p)
|
|
81 {
|
|
82 if (!p) return 1;
|
|
83 for(;*p;p++) {
|
|
84 char c = *p;
|
|
85 if (c<=' ') return 1;
|
|
86 if (('a'<=c && c<='z') ||
|
|
87 ('0'<=c && c<='9') ||
|
|
88 ('/'==c ) ||
|
|
89 ('-'==c )) continue;
|
|
90 return 1;
|
|
91 printf("%c", c);
|
|
92 }
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 void
|
|
97 usage()
|
|
98 {
|
1
|
99 printf("Usage:\n\trun: run process\n\tbuild: build docker process from Dockerfile\n\tattach: atach process\n\tdettach: \n\tpull: \n\timages: \n\tcommit:\n");
|
0
|
100 printf(" ps-name should be students/e11/e115711/01 or teachers/kono/02\n");
|
|
101 }
|
|
102
|
|
103 /* main(int argc, char **argv) - main process loop */
|
|
104
|
|
105 int main(int argc, char **argv)
|
|
106 {
|
|
107 int gid;
|
|
108 int uid;
|
|
109
|
|
110 /* Set euid and egid to actual user */
|
|
111
|
|
112 char *name = getlogin();
|
|
113 uid = getuid();
|
|
114 gid = getgid();
|
|
115 printf("uid %d gid %d name %s\n", uid,gid,name);
|
|
116 setegid(getgid());
|
|
117 seteuid(getuid());
|
|
118
|
|
119 regex_t *pattern = NEW(regex_t);
|
|
120 if (regcomp(pattern, name, 0) != 0) {
|
|
121 exit(0);
|
|
122 }
|
|
123
|
|
124 /* Confirm user is in GROUP(999) group */
|
|
125
|
|
126 /*
|
|
127 if ( gid != 999 ) {
|
|
128 printf("User Not Authorized! Exiting...\n");
|
|
129 exit(1);
|
|
130 }
|
|
131 */
|
|
132
|
|
133 /* Set uid, gid, euid and egid to root */
|
|
134
|
|
135 setegid(0);
|
|
136 seteuid(0);
|
|
137 setgid(0);
|
|
138 setuid(0);
|
|
139
|
1
|
140 if (argc >= 3) {
|
|
141 if ( strncmp(argv[1], pull_command, 6) == 0 ) {
|
0
|
142 if (regexec(pattern, argv[2], (size_t) 0, NULL, 0)) {
|
|
143 fprintf(stderr, bad_name);
|
|
144 exit(0);
|
|
145 }
|
1
|
146
|
0
|
147 if (check_name(argv[2])) {
|
|
148 fprintf(stderr, bad_name);
|
|
149 exit(0);
|
|
150 }
|
1
|
151
|
0
|
152 char exec[1024];
|
1
|
153
|
|
154 strncpy(exec,"/usr/local/bin/newps.py -c /etc/libvirt/qemu/fedora19.xml -n ", 900);
|
|
155
|
0
|
156 strncat(exec, argv[2],1000);
|
|
157 fprintf(stdout, "excuting %s\n",exec );
|
|
158 system(exec);
|
|
159 }
|
|
160 }
|
|
161
|
|
162
|
|
163 PSLISTPTR pslist = get_pslist(pattern);
|
|
164
|
|
165 char name_xml[1024];
|
|
166 name_xml[0] = 0;
|
|
167 if (argc>=3) {
|
|
168 if ( strncmp(argv[1], "define", 6) == 0 ) {
|
|
169 strncpy(name_xml,argv[2],900);
|
|
170 strncat(name_xml,".xml",1000);
|
|
171 } else if (check_pslist_name(pslist, argv[2])==0) {
|
|
172 fprintf(stderr, bad_name);
|
|
173 print_pslist(pslist);
|
|
174 exit(0);
|
|
175 }
|
|
176 } else if (argc<2) {
|
|
177 print_pslist(pslist);
|
|
178 usage();
|
|
179 exit(0);
|
|
180 }
|
|
181
|
|
182 /* Check argv for proper arguments and run
|
|
183 * the corresponding script, if invoked.
|
|
184 */
|
|
185
|
|
186 if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) {
|
|
187 print_pslist(pslist);
|
1
|
188 } else if (strncmp(argv[1], run_command, 5) == 0) {
|
|
189 if (execl(command, command, run_command, argv[2], NULL) < 0) {
|
|
190 perror("Execl:");
|
|
191 }
|
0
|
192 } else if (strncmp(argv[1], start_command, 5) == 0) {
|
|
193 if (execl(command, command, start_command, argv[2], NULL) < 0) {
|
|
194 perror("Execl:");
|
|
195 }
|
|
196 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
|
|
197 if (execl(command, command, stop_command, argv[2], NULL) < 0) {
|
|
198 perror("Execl:");
|
|
199 }
|
1
|
200 } else if ( strncmp(argv[1], build_command, 8) == 0 ) {
|
|
201 if (execl(command, command, build_command, argv[2], NULL) < 0) {
|
0
|
202 perror("Execl:");
|
|
203 }
|
1
|
204 } else if (strncmp(argv[1], attach_command, 6) == 0 ) {
|
|
205 if (execl(command, command, attach_command, name_xml, NULL) < 0) {
|
0
|
206 perror("Execl:");
|
|
207 }
|
1
|
208 } else if ( strncmp(argv[1], dettach_command, 8) == 0 ) {
|
|
209 if (execl(command, command, dettach_command, argv[2], NULL) < 0) {
|
0
|
210 perror("Execl:");
|
|
211 }
|
|
212 } else {
|
|
213 usage();
|
|
214 exit(1);
|
|
215 }
|
|
216 exit(0);
|
|
217 }
|
|
218
|
|
219 /* end */
|