Mercurial > hg > Applications > docker-wrapper
changeset 6:b87ef4d884af
add auto define ps name.
author | taiki <taiki@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Sep 2014 06:32:15 -1000 |
parents | f4e5b5c0d621 |
children | 401d1b8af977 |
files | ie-docker.c |
diffstat | 1 files changed, 109 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/ie-docker.c Mon Jul 14 22:46:28 2014 -1000 +++ b/ie-docker.c Tue Sep 02 06:32:15 2014 -1000 @@ -30,6 +30,24 @@ static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n"; +const char *guests[] = {"mata"}; +const char *managers[] = {"taiki"}; +const char students_sym[] = "students"; +const char managers_sym[] = "managers"; +const char guests_sym[] = "guests"; +const char delimiter[] = "_"; + +enum { + NAME_LENGTH = 50, + VM_NAME_LENGTH = 50 +}; + +enum { + STUDENTS, + GUESTS, + MANAGERS +}; + #define PSNAME_MAX (512) typedef struct pslist { @@ -93,6 +111,82 @@ return 0; } +int +check_user_name(const char *account_name) +{ + const char *regex = "[ek]([0-9{6})"; + + regex_t *pattern = NEW(regex_t); + int ret = -1; + + if (regcomp(pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) { + exit(0); + } + + if (!ret) { + return STUDENTS; + } + + ret = regexec(pattern, account_name, (size_t) 0, NULL, 0); + regfree(pattern); + + const int managers_num = sizeof(managers) / sizeof(managers[0]); + int i = 0; + + for (; i< managers_num; i++) { + if (strncmp(account_name, managers[i], NAME_LENGTH) == 0) { + return MANAGERS; + } + } + + const int guests_num = sizeof(guests) / sizeof(guests[0]); + int j = 0; + + for (; j< guests_num; j++) { + if (strncmp(account_name, guests[j], NAME_LENGTH) == 0) { + return GUESTS; + } + } + + return -1; +} + +void +bind_name(char *name, const char *first, const char *second) +{ + strncat(name, first, VM_NAME_LENGTH); + strncat(name, delimiter, VM_NAME_LENGTH); + strncat(name, second, VM_NAME_LENGTH); + strncat(name, delimiter, VM_NAME_LENGTH); + return; +} + +void +make_vm_name(char *vm_name, const int account_type, const char *account_name, const char *vm_num) +{ + switch(account_type) { + case STUDENTS: + strncat(vm_name, students_sym, VM_NAME_LENGTH); + strncat(vm_name, delimiter, VM_NAME_LENGTH); + strncat(vm_name, account_name, 3); + strncat(vm_name, delimiter, VM_NAME_LENGTH); + strncat(vm_name, account_name, VM_NAME_LENGTH); + strncat(vm_name, delimiter, VM_NAME_LENGTH); + break; + case GUESTS: + bind_name(vm_name, guests_sym, account_name); + break; + case MANAGERS: + bind_name(vm_name, managers_sym, account_name); + break; + default : + fprintf(stderr, "Error: no registered type name."); + return; + } + + strncat(vm_name, vm_num, VM_NAME_LENGTH); +} + void usage() { @@ -101,7 +195,6 @@ printf("\tbuild: build docker process from Dockerfile\n"); printf("\tattach: atach process\n"); printf("\tdettach: \n"); - printf("\tpull: \n"); printf("\timages: \n"); printf("\tcommit:\n"); printf("\tps-name should be students_e11_e115711_01 or teachers_kono_02\n"); @@ -130,11 +223,12 @@ setegid(getgid()); seteuid(getuid()); - regex_t *pattern = NEW(regex_t); - if (regcomp(pattern, name, 0) != 0) { - exit(0); + int account_type = check_user_name(name); + if (account_type < 0) { + fprintf(stderr, "Permission denied. :%s\n", name); } + /* Confirm user is in GROUP(999) group */ /* @@ -146,11 +240,22 @@ /* Set uid, gid, euid and egid to root */ + regex_t *pattern = NEW(regex_t); + if (regcomp(pattern, name, 0) != 0) { + exit(0); + } + setegid(0); seteuid(0); setgid(0); setuid(0); + char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); + vm_name[0] = '\0'; + if (strncmp(argv[1], "list", 4) != 0) { + make_vm_name(vm_name, account_type, name, argv[2]); + } + if (argc >= 3) { if (strncmp(argv[1], run_command, 6) == 0 ) { if (strncmp(argv[3], "--name", 6) != 0) { @@ -167,16 +272,6 @@ fprintf(stderr, bad_name); exit(0); } - - /* - char exec[1024]; - - strncpy(exec,"/usr/local/bin/newps.py -c /etc/libvirt/qemu/fedora19.xml -n ", 900); - - strncat(exec, argv[2],1000); - fprintf(stdout, "excuting %s\n",exec ); - system(exec); - */ } }