view ie-docker.c @ 4:e802f3c8af1a

fix ie-docker can execute "docker run" command.
author taiki <taiki@cr.ie.u-ryukyu.ac.jp>
date Mon, 14 Jul 2014 22:45:54 -1000
parents 3e0e4fd65313
children b87ef4d884af
line wrap: on
line source



#include <stdlib.h>
#include <unistd.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

#include <sys/types.h>
#include <regex.h>

/* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */

#define command "/usr/bin/docker"
#define ps_command "/usr/bin/docker ps -a"
#define run_command "run"
#define build_command "build"
#define attach_command "attach"
#define dettach_command "dettach"
#define pull_command "pull" /* download docker image command */
#define images_command "images" /* list images command */
#define commit_command "commit" /* make image command */
#define rm_command "rm" /* remove container command */
#define rmi_command "rmi" /* remove image command */
#define start_command "start" 
#define stop_command "stop" 

static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n";

#define PSNAME_MAX (512)

typedef struct pslist {
    char name[PSNAME_MAX];
    struct pslist *next;
} PSLIST, *PSLISTPTR;

#define NEW(type)  ((type*)malloc(sizeof(type)))

/* Define global variables */

PSLISTPTR
get_pslist(regex_t *list_pattern)
{
    PSLISTPTR list = NEW(PSLIST);
    PSLISTPTR p = list;
    p->name[0] = 0;
    p->next = 0;
    FILE *fp = popen(ps_command,"r");
    while(fgets(p->name,PSNAME_MAX,fp)!=NULL) {
        if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue;
        p->next = NEW(PSLIST);
        p = p->next;
    }
    p->name[0] = 0;
    pclose(fp);
    return list;
}

void 
print_pslist(PSLISTPTR list) 
{
    for(;list && list->name[0]; list = list->next) {
        fprintf(stdout, "   %s\n",list->name);
    }
}

int
check_pslist_name(PSLISTPTR list, char *arg)
{
    for(;list && list->name[0]; list = list->next) {
        if (strstr(list->name,arg)!=0) return 1;
    }
    return 0;
}

int
check_name(const char *p)
{
    if (!p) return  1;
    for(;*p;p++) {
        char c = *p;
        if (c<=' ') return 1;
        if (('a'<=c && c<='z') ||
                ('0'<=c && c<='9') ||
                ('_'==c ) ||
                ('-'==c )) continue;
        return 1;
        printf("%c", c);
    }
    return 0;
}

void 
usage()
{
    printf("Usage: ie-docker\n");
    printf("\trun:    run process\n");
    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");
}

void
run_usage()
{
    printf("Usage: ie-docker run\n");
    printf("\tie-docker [option] --name e145701_[process_name] {image name}:{tag} [execute command] [argument]");
}

/* main(int argc, char **argv) - main process loop */

int main(int argc, char **argv)
{
    int gid;
    int uid;

    /* Set euid and egid to actual user */

    char *name = getlogin();
    uid = getuid();
    gid = getgid();
    printf("uid %d gid %d name %s\n", uid,gid,name);
    setegid(getgid());
    seteuid(getuid());

    regex_t *pattern = NEW(regex_t);
    if (regcomp(pattern, name, 0) != 0) {
        exit(0);
    }

    /* Confirm user is in GROUP(999) group */

    /*
       if ( gid != 999 ) {
       printf("User Not Authorized!  Exiting...\n");
       exit(1);
       }
     */

    /* Set uid, gid, euid and egid to root */

    setegid(0);
    seteuid(0);
    setgid(0);
    setuid(0);

    if (argc >= 3) {
        if (strncmp(argv[1], run_command, 6) == 0 ) {
            if (strncmp(argv[3], "--name", 6) != 0) {
                run_usage();
                exit(0);
            }

            if (regexec(pattern, argv[4], (size_t) 0, NULL, 0)) {
                fprintf(stderr, bad_name);
                exit(0);
            }

            if (check_name(argv[3])) {
                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);
            */
        }
    }


    PSLISTPTR pslist = get_pslist(pattern);

    /* Check argv for proper arguments and run 
     * the corresponding script, if invoked.
     */

    if (argv[1]==0 || strncmp(argv[1], "ps", 4) == 0 ) {
        print_pslist(pslist);
    } else if (strncmp(argv[1], run_command, 5) == 0) {
        if (execl(command, command, run_command, argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], NULL) < 0) {
            perror("Execl:");
        }
    } else if (strncmp(argv[1], start_command, 5) == 0) {
        if (execl(command, command, start_command, argv[2], NULL) < 0) {
            perror("Execl:");
        }
    } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
        if (execl(command, command, stop_command, argv[2], NULL) < 0) {
            perror("Execl:");
        }
    } else if ( strncmp(argv[1], build_command, 8) == 0 ) {
        if (execl(command, command, build_command, argv[2], NULL) < 0) {
            perror("Execl:");
        }
    } else if (strncmp(argv[1], attach_command, 6) == 0 ) {
        if (execl(command, command, attach_command, NULL) < 0) {
            perror("Execl:");
        }
    } else if ( strncmp(argv[1], dettach_command, 8) == 0 ) {
        if (execl(command, command, dettach_command, argv[2], NULL) < 0) {
            perror("Execl:");
        }
    } else {
        usage();
        exit(1);
    }
    exit(0);
}

/* end */