Mercurial > hg > Applications > ie-cloud
comparison ie-cloud.c @ 0:61bc8410d480
add files
author | taiki |
---|---|
date | Wed, 11 Feb 2015 20:23:09 +0900 |
parents | |
children | 8de3ca550e8f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61bc8410d480 |
---|---|
1 #include <stdlib.h> | |
2 #include <unistd.h> | |
3 | |
4 #include <stdio.h> | |
5 #include <sys/types.h> | |
6 #include <unistd.h> | |
7 #include <signal.h> | |
8 #include <string.h> | |
9 | |
10 #include <sys/types.h> | |
11 #include <regex.h> | |
12 | |
13 #include "ie-cloud.h" | |
14 | |
15 | |
16 void | |
17 get_port_number(const char *user_name, char const *project_name, char *port_number) | |
18 { | |
19 FILE *fp = NULL; | |
20 if ((fp = fopen(portlist_file, "r")) == NULL) { | |
21 printf("file open error\n"); | |
22 } | |
23 | |
24 // file format: portnumber,username,projectname | |
25 char buff[BUFF_SIZE]; | |
26 char *port; | |
27 int user_name_flag = 1; | |
28 int project_name_flag = 1; | |
29 while (fgets(buff, BUFF_SIZE, fp) != NULL) { | |
30 buff[strlen(buff) - 1] = '\0'; | |
31 | |
32 port = strtok(buff, ","); | |
33 | |
34 char *ret = strtok(NULL, ","); | |
35 if (ret == NULL) continue; | |
36 user_name_flag = strncmp(user_name, ret, BUFF_SIZE); | |
37 | |
38 ret = strtok(NULL, ","); | |
39 if (ret == NULL) continue; | |
40 project_name_flag = strncmp(project_name, ret, BUFF_SIZE); | |
41 | |
42 if (user_name_flag == 0 && project_name_flag == 0) { | |
43 printf("port :%s\n", port); | |
44 strncpy(port_number, port, PORT_LENGTH); | |
45 break; | |
46 } | |
47 } | |
48 fclose(fp); | |
49 if (user_name_flag == 1 || project_name_flag == 1) { | |
50 printf("[!] can't get port number for %s", user_name); | |
51 exit(1); | |
52 } | |
53 } | |
54 | |
55 | |
56 void | |
57 parse_run_command(const int argc, char **argv, run_command_opt *opt) | |
58 { | |
59 int i = 2; | |
60 int parse_image_flag = 0; | |
61 for (i = 2; i < argc; i++) { | |
62 if(strncmp(argv[i], "--name", 6) == 0) { // process name | |
63 strncpy(opt->ps_name, argv[i + 1], 16); | |
64 i++; | |
65 } else if (argv[i][0] == '-') { | |
66 if (argv[i][1] == 't') { | |
67 opt->tty = TRUE; | |
68 } else if (argv[i][1] == 'i') { | |
69 opt->interactive = TRUE; | |
70 } else if (argv[i][1] == 'd') { | |
71 opt->dettach = TRUE; | |
72 } else if (argv[i][1] == 'v') { | |
73 strncpy(opt->volume, argv[i + 1], 128); | |
74 i++; | |
75 } else if (argv[i][1] == 'p') { | |
76 strncpy(opt->innerport, argv[i + 1], 16); | |
77 i++; | |
78 } | |
79 } else if (parse_image_flag) { // image name | |
80 strncpy(opt->exec_ps_command, argv[i], 64); | |
81 } else { // image name | |
82 parse_image_flag = 1; | |
83 strncpy(opt->image_name, argv[i], 16); | |
84 } | |
85 } | |
86 /* | |
87 printf("run command opt ::memory-%s innerport-%s outerport-%s tty-%d dettach-%d interactive-%d ps_name-%s exec_ps_command-%s volume-%s image-name-%s\n", | |
88 opt->memory, | |
89 opt->innerport, | |
90 opt->outerport, | |
91 opt->tty, | |
92 opt->dettach, | |
93 opt->interactive, | |
94 opt->ps_name, | |
95 opt->exec_ps_command, | |
96 opt->volume, | |
97 opt->image_name); | |
98 */ | |
99 } | |
100 | |
101 PSLISTPTR | |
102 get_pslist(regex_t *list_pattern) | |
103 { | |
104 PSLISTPTR list = NEW(PSLIST); | |
105 PSLISTPTR p = list; | |
106 p->name[0] = 0; | |
107 p->next = 0; | |
108 FILE *fp = popen(ps_command,"r"); | |
109 while(fgets(p->name,PSNAME_MAX,fp)!=NULL) { | |
110 if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue; | |
111 p->next = NEW(PSLIST); | |
112 p = p->next; | |
113 } | |
114 p->name[0] = 0; | |
115 pclose(fp); | |
116 | |
117 return list; | |
118 } | |
119 | |
120 void | |
121 print_pslist(PSLISTPTR list) | |
122 { | |
123 for(;list && list->name[0]; list = list->next) { | |
124 fprintf(stdout, " %s\n",list->name); | |
125 } | |
126 } | |
127 | |
128 int | |
129 check_pslist_name(PSLISTPTR list, char *arg) | |
130 { | |
131 for(;list && list->name[0]; list = list->next) { | |
132 if (strstr(list->name,arg)!=0) return 1; | |
133 } | |
134 | |
135 return 0; | |
136 } | |
137 | |
138 int | |
139 check_name(const char *p) | |
140 { | |
141 if (!p) return 1; | |
142 for(;*p;p++) { | |
143 char c = *p; | |
144 if (c<=' ') return 1; | |
145 if (('a'<=c && c<='z') || | |
146 ('0'<=c && c<='9') || | |
147 ('_'==c ) || | |
148 ('-'==c )) continue; | |
149 return 1; | |
150 printf("%c", c); | |
151 } | |
152 return 0; | |
153 } | |
154 | |
155 int | |
156 check_user_name(const char *account_name) | |
157 { | |
158 const char *regex = "[ek]([0-9]{6})"; | |
159 | |
160 regex_t *pattern = NEW(regex_t); | |
161 int ret = 1; | |
162 | |
163 if (regcomp(pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) { | |
164 exit(0); | |
165 } | |
166 | |
167 ret = regexec(pattern, account_name, (size_t) 0, NULL, 0); | |
168 regfree(pattern); | |
169 | |
170 if (!ret) { | |
171 return STUDENTS; | |
172 } | |
173 | |
174 ret = regexec(pattern, account_name, (size_t) 0, NULL, 0); | |
175 regfree(pattern); | |
176 | |
177 const int managers_num = sizeof(managers) / sizeof(managers[0]); | |
178 int i = 0; | |
179 | |
180 for (; i< managers_num; i++) { | |
181 if (strncmp(account_name, managers[i], NAME_LENGTH) == 0) { | |
182 return MANAGERS; | |
183 } | |
184 } | |
185 | |
186 const int guests_num = sizeof(guests) / sizeof(guests[0]); | |
187 int j = 0; | |
188 | |
189 for (; j< guests_num; j++) { | |
190 if (strncmp(account_name, guests[j], NAME_LENGTH) == 0) { | |
191 return GUESTS; | |
192 } | |
193 } | |
194 | |
195 return -1; | |
196 } | |
197 | |
198 void | |
199 bind_name(char *name, const char *first, const char *second) | |
200 { | |
201 strncat(name, first, PS_NAME_LENGTH); | |
202 strncat(name, delimiter, PS_NAME_LENGTH); | |
203 strncat(name, second, PS_NAME_LENGTH); | |
204 strncat(name, delimiter, PS_NAME_LENGTH); | |
205 return; | |
206 } | |
207 | |
208 void | |
209 make_ps_name(char *ps_name, const int account_type, const char *account_name, const char *vm_num) | |
210 { | |
211 switch(account_type) { | |
212 case STUDENTS: | |
213 strncat(ps_name, students_sym, PS_NAME_LENGTH); | |
214 strncat(ps_name, delimiter, PS_NAME_LENGTH); | |
215 strncat(ps_name, account_name, 3); | |
216 strncat(ps_name, delimiter, PS_NAME_LENGTH); | |
217 strncat(ps_name, account_name, PS_NAME_LENGTH); | |
218 strncat(ps_name, delimiter, PS_NAME_LENGTH); | |
219 break; | |
220 case GUESTS: | |
221 bind_name(ps_name, guests_sym, account_name); | |
222 break; | |
223 case MANAGERS: | |
224 bind_name(ps_name, managers_sym, account_name); | |
225 break; | |
226 default : | |
227 fprintf(stderr, "[!] Error: no registered type name."); | |
228 return; | |
229 } | |
230 | |
231 strncat(ps_name, vm_num, PS_NAME_LENGTH); | |
232 } | |
233 | |
234 void | |
235 usage() | |
236 { | |
237 printf("Usage: ie-docker\n"); | |
238 printf("\trun: run process\n"); | |
239 printf("\tbuild: build docker process from Dockerfile\n"); | |
240 printf("\tattach: attach process\n"); | |
241 printf("\tdettach: dettach process\n"); | |
242 printf("\timages: list images\n"); | |
243 printf("\tcommit: \n"); | |
244 } | |
245 | |
246 void | |
247 run_usage() | |
248 { | |
249 printf("Usage:\tie-docker run\n"); | |
250 printf("\tie-docker [option] --name [ps_name] {image name}:{tag} [execute command] [argument]"); | |
251 } | |
252 | |
253 /* main(int argc, char **argv) - main process loop */ | |
254 | |
255 int main(int argc, char **argv) | |
256 { | |
257 int gid; | |
258 int uid; | |
259 | |
260 /* Set euid and egid to actual user */ | |
261 | |
262 char *name = getlogin(); | |
263 uid = getuid(); | |
264 gid = getgid(); | |
265 printf("uid %d gid %d name %s\n", uid,gid,name); | |
266 setegid(getgid()); | |
267 seteuid(getuid()); | |
268 | |
269 int account_type = check_user_name(name); | |
270 if (account_type < 0) { | |
271 fprintf(stderr, "[!] Permission denied. :%s\n", name); | |
272 } | |
273 | |
274 /* Confirm user is in GROUP(999) group */ | |
275 | |
276 /* | |
277 if ( gid != 999 ) { | |
278 printf("User Not Authorized! Exiting...\n"); | |
279 exit(1); | |
280 } | |
281 */ | |
282 | |
283 /* Set uid, gid, euid and egid to root */ | |
284 | |
285 regex_t *pattern = NEW(regex_t); | |
286 if (regcomp(pattern, name, 0) != 0) { | |
287 exit(0); | |
288 } | |
289 | |
290 setegid(0); | |
291 seteuid(0); | |
292 setgid(0); | |
293 setuid(0); | |
294 | |
295 if (strncmp(argv[1], create_command, 6) == 0) { | |
296 char exec[512]; | |
297 sprintf(exec, "/usr/local/bin/create.py %s", argv[2]); | |
298 system(exec); | |
299 exit(1); | |
300 } | |
301 | |
302 char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH); | |
303 if (ps_name == NULL) { | |
304 printf("[!] malloc error."); | |
305 } | |
306 ps_name[0] = '\0'; | |
307 | |
308 run_command_opt *opt = (run_command_opt *)malloc(sizeof(run_command_opt)); | |
309 if (opt == NULL) { | |
310 printf("[!] malloc error."); | |
311 } | |
312 opt->tty = FALSE; | |
313 opt->dettach = FALSE; | |
314 opt->interactive = FALSE; | |
315 | |
316 if (strncmp(argv[1], "ps", 4) != 0) { | |
317 if (strncmp(argv[1], "run", 3) == 0) { | |
318 parse_run_command(argc, argv, opt); | |
319 if (check_name(opt->ps_name)) { | |
320 fprintf(stderr, bad_name); | |
321 exit(0); | |
322 } | |
323 get_port_number(name, opt->ps_name, opt->outerport); | |
324 strncpy(ps_name, opt->ps_name, 64); | |
325 opt->ps_name[0] = '\0'; | |
326 make_ps_name(opt->ps_name, account_type, name, ps_name); | |
327 } else { | |
328 make_ps_name(ps_name, account_type, name, argv[2]); | |
329 } | |
330 printf("process name : %s\n", opt->ps_name); | |
331 } | |
332 | |
333 PSLISTPTR pslist = get_pslist(pattern); | |
334 | |
335 /* | |
336 * Check argv for proper arguments and run | |
337 * the corresponding script, if invoked. | |
338 */ | |
339 | |
340 if (argv[1]==0 || strncmp(argv[1], "ps", 4) == 0 ) { | |
341 print_pslist(pslist); | |
342 } else if (strncmp(argv[1], run_command, 5) == 0) { | |
343 | |
344 char *args[16]; | |
345 int i = 0; | |
346 | |
347 args[i++] = command; | |
348 args[i++] = run_command; | |
349 if (opt->dettach) args[i++] = "-d"; | |
350 if (opt->tty) args[i++] = "-t"; | |
351 if (opt->interactive) args[i++] = "-i"; | |
352 args[i++] = "-m"; | |
353 args[i++] = "512m"; | |
354 args[i++] = "-v"; | |
355 args[i++] = opt->volume; | |
356 args[i++] = "-p"; | |
357 char port[32]; | |
358 sprintf(port, "%s:%s", opt->outerport, opt->innerport); | |
359 args[i++] = port; | |
360 args[i++] = "--name"; | |
361 args[i++] = opt->ps_name; | |
362 args[i++] = opt->image_name; | |
363 args[i++] = opt->exec_ps_command; | |
364 args[i++] = NULL; | |
365 /* | |
366 printf("run command opt ::memory-%s innerport-%s outerport-%s tty-%d dettach-%d interactive-%d ps_name-%s exec_ps_command-%s volume-%s image-name-%s\n", | |
367 opt->memory, | |
368 opt->innerport, | |
369 opt->outerport, | |
370 opt->tty, | |
371 opt->dettach, | |
372 opt->interactive, | |
373 opt->ps_name, | |
374 opt->exec_ps_command, | |
375 opt->volume, | |
376 opt->image_name); | |
377 | |
378 */ | |
379 if (execv(args[0], args) < 0) { | |
380 perror("[!] Execv:"); | |
381 } | |
382 } else if (strncmp(argv[1], start_command, 5) == 0) { | |
383 if (execl(command, command, start_command, ps_name, NULL) < 0) { | |
384 perror("[!] Execl:"); | |
385 } | |
386 } else if (strncmp(argv[1], exec_command, 5) == 0) { | |
387 if (execl(command, command, exec_command, argv[2], argv[3], argv[4], NULL) < 0) { | |
388 perror("[!] Execl:"); | |
389 } | |
390 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { | |
391 if (execl(command, command, stop_command, ps_name, NULL) < 0) { | |
392 perror("[!] Execl:"); | |
393 } | |
394 } else if ( strncmp(argv[1], build_command, 8) == 0 ) { | |
395 if (execl(command, command, build_command, argv[2], NULL) < 0) { | |
396 perror("[!] Execl:"); | |
397 } | |
398 } else if (strncmp(argv[1], attach_command, 6) == 0 ) { | |
399 if (execl(command, command, attach_command, ps_name, NULL) < 0) { | |
400 perror("[!] Execl:"); | |
401 } | |
402 } else if ( strncmp(argv[1], rm_command, 2) == 0 ) { | |
403 if (execl(command, command, rm_command, ps_name, NULL) < 0) { | |
404 perror("[!] Execl:"); | |
405 } | |
406 } else { | |
407 usage(); | |
408 } | |
409 free(ps_name); | |
410 free(opt); | |
411 exit(0); | |
412 } | |
413 | |
414 /* end */ |