Mercurial > hg > Applications > virsh-wrapper
changeset 21:13646e64d98b
fix indent
author | taiki <taiki@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 05 Apr 2014 04:15:57 +0900 |
parents | 07ef2fc111a2 |
children | daa7279b0d78 |
files | ie-virsh.c |
diffstat | 1 files changed, 115 insertions(+), 116 deletions(-) [+] |
line wrap: on
line diff
--- a/ie-virsh.c Mon Feb 10 04:47:52 2014 -1000 +++ b/ie-virsh.c Sat Apr 05 04:15:57 2014 +0900 @@ -1,4 +1,13 @@ +/******************************************** + * Virsh Wrapper - Secure Yourself * + * * + * 2007 - Mike Golvach - eggi@comcast.net * + * 2012 - Shinji KONO kono@ie.u-rykyu.ac.jp * + * * + * Usage: COMMAND [start|stop] * + * * + ********************************************/ #include <stdlib.h> #include <unistd.h> @@ -12,17 +21,7 @@ #include <sys/types.h> #include <regex.h> -/******************************************** - * Virsh Wrapper - Secure Yourself * - * * - * 2007 - Mike Golvach - eggi@comcast.net * - * 2012 - Shinji KONO kono@ie.u-rykyu.ac.jp * - * * - * Usage: COMMAND [start|stop] * - * * - ********************************************/ - - /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ +/* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ /* #define command "/usr/bin/virsh" */ #define command "/usr/local/bin/virsh" @@ -47,7 +46,7 @@ /* Define global variables */ -VMLISTPTR + VMLISTPTR get_vmlist(regex_t *list_pattern) { VMLISTPTR list = NEW(VMLIST); @@ -65,7 +64,7 @@ return list; } -void + void print_vmlist(VMLISTPTR list) { for(;list && list->name[0]; list=list->next) { @@ -73,7 +72,7 @@ } } -int + int check_vmlist_name(VMLISTPTR list, char *arg) { for(;list && list->name[0]; list=list->next) { @@ -82,28 +81,28 @@ return 0; } -int + 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; + 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 + void usage() { - printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine] [vm-name]\n"); - printf(" vm-name should be students/e11/e115711/01 or teachers/kono/02\n"); + printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine] [vm-name]\n"); + printf(" vm-name should be students/e11/e115711/01 or teachers/kono/02\n"); } /* main(int argc, char **argv) - main process loop */ @@ -113,108 +112,108 @@ int gid; int uid; -/* Set euid and egid to actual user */ + /* 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()); + 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); - } + regex_t *pattern = NEW(regex_t); + if (regcomp(pattern, name, 0) != 0) { + exit(0); + } -/* Confirm user is in GROUP(999) group */ + /* Confirm user is in GROUP(999) group */ -/* - if ( gid != 999 ) { - printf("User Not Authorized! Exiting...\n"); - exit(1); - } - */ + /* + if ( gid != 999 ) { + printf("User Not Authorized! Exiting...\n"); + exit(1); + } + */ -/* Set uid, gid, euid and egid to root */ + /* Set uid, gid, euid and egid to root */ + + setegid(0); + seteuid(0); + setgid(0); + setuid(0); - setegid(0); - seteuid(0); - setgid(0); - setuid(0); - - if (argc>=3) { - if ( strncmp(argv[1], "define", 6) == 0 ) { - if (regexec(pattern, argv[2], (size_t) 0, NULL, 0)) { - fprintf(stderr, bad_name); - exit(0); + if (argc>=3) { + if ( strncmp(argv[1], "define", 6) == 0 ) { + if (regexec(pattern, argv[2], (size_t) 0, NULL, 0)) { + fprintf(stderr, bad_name); + exit(0); + } + if (check_name(argv[2])) { + fprintf(stderr, bad_name); + exit(0); + } + char exec[1024]; + // strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora16.xml -n ",900); + strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora19.xml -n ",900); + strncat(exec, argv[2],1000); + fprintf(stdout, "excuting %s\n",exec ); + system(exec); } - if (check_name(argv[2])) { - fprintf(stderr, bad_name); - exit(0); - } - char exec[1024]; - // strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora16.xml -n ",900); - strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora19.xml -n ",900); - strncat(exec, argv[2],1000); - fprintf(stdout, "excuting %s\n",exec ); - system(exec); } - } - VMLISTPTR vmlist = get_vmlist(pattern); + VMLISTPTR vmlist = get_vmlist(pattern); - char name_xml[1024]; - name_xml[0] = 0; - if (argc>=3) { - if ( strncmp(argv[1], "define", 6) == 0 ) { - strncpy(name_xml,argv[2],900); - strncat(name_xml,".xml",1000); - } else if (check_vmlist_name(vmlist, argv[2])==0) { - fprintf(stderr, bad_name); - print_vmlist(vmlist); - exit(0); - } - } else if (argc<2) { + char name_xml[1024]; + name_xml[0] = 0; + if (argc>=3) { + if ( strncmp(argv[1], "define", 6) == 0 ) { + strncpy(name_xml,argv[2],900); + strncat(name_xml,".xml",1000); + } else if (check_vmlist_name(vmlist, argv[2])==0) { + fprintf(stderr, bad_name); + print_vmlist(vmlist); + exit(0); + } + } else if (argc<2) { print_vmlist(vmlist); usage(); exit(0); - } + } -/* Check argv for proper arguments and run - * the corresponding script, if invoked. - */ + /* Check argv for proper arguments and run + * the corresponding script, if invoked. + */ - if ( argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { - print_vmlist(vmlist); - } else if ( strncmp(argv[1], "start", 5) == 0 ) { - if (execl(command, command, start_command, argv[2], NULL) < 0) { - perror("Execl:"); - } - } else if ( strncmp(argv[1], "destroy", 4) == 0 ) { - if (execl(command, command, stop_command, argv[2], NULL) < 0) { - perror("Execl:"); - } - } else if ( strncmp(argv[1], "dumpxml", 7) == 0 ) { - if (execl(command, command, dumpxml_command, argv[2], NULL) < 0) { - perror("Execl:"); - } - } else if ( strncmp(argv[1], "define", 6) == 0 ) { - chdir("/usr/local/etc/libvirt/qemu"); - if (execl(command, command, define_command, name_xml, NULL) < 0) { - perror("Execl:"); - } - } else if ( strncmp(argv[1], "undefine", 8) == 0 ) { - chdir("/usr/local/etc/libvirt/qemu"); - if (execl(command, command, undefine_command, argv[2], NULL) < 0) { - perror("Execl:"); - } - } else { - usage(); - exit(1); - } - exit(0); + if ( argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { + print_vmlist(vmlist); + } else if ( strncmp(argv[1], "start", 5) == 0 ) { + if (execl(command, command, start_command, argv[2], NULL) < 0) { + perror("Execl:"); + } + } else if ( strncmp(argv[1], "destroy", 4) == 0 ) { + if (execl(command, command, stop_command, argv[2], NULL) < 0) { + perror("Execl:"); + } + } else if ( strncmp(argv[1], "dumpxml", 7) == 0 ) { + if (execl(command, command, dumpxml_command, argv[2], NULL) < 0) { + perror("Execl:"); + } + } else if ( strncmp(argv[1], "define", 6) == 0 ) { + chdir("/usr/local/etc/libvirt/qemu"); + if (execl(command, command, define_command, name_xml, NULL) < 0) { + perror("Execl:"); + } + } else if ( strncmp(argv[1], "undefine", 8) == 0 ) { + chdir("/usr/local/etc/libvirt/qemu"); + if (execl(command, command, undefine_command, argv[2], NULL) < 0) { + perror("Execl:"); + } + } else { + usage(); + exit(1); + } + exit(0); } /* end */