view ie-vagrant.c @ 16:d8ea207162ca

add box command, and box add, box list
author taiki
date Mon, 18 Nov 2013 03:50:20 -1000
parents f194adc597b6
children fb9f3738a8e6
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>

#include <time.h>

/********************************************
 * Vagrant Wrapper - Secure Yourself          *
 *                                          *
 * 2007 - Mike Golvach - eggi@comcast.net   *
 * 2013 - Shinji KONO  kono@ie.u-rykyu.ac.jp *
 *                                          *
 * Usage: COMMAND [init|up|destroy|ssh]              *
 *                                          *
 ********************************************/
 
 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */

#define command "/usr/bin/vagrant"
#define init_command "init"
#define up_command "up"
#define destroy_command "destroy"
#define ssh_command "ssh"
#define box_command "box"

#define box_add_command "add"
#define box_list_command "list"

#define provider_arg "--provider=kvm"

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

#define VAGRANT_FILE "Vagrantfile"

/* Define global variables */

void 
usage()
{
  printf("Usage: COMMAND [init|up|destroy|ssh]\n");
}

void
exec_init(int uid, int gid)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(-1);
    } else if (pid == 0) {
        if (execl(command, command, init_command, NULL) < 0) {
            perror("Execl:");
        }
    } else {
        sleep(2);
        if (chown(VAGRANT_FILE, uid, gid) != 0) {
            printf("chown error.\n");
            exit(1);
        } 
        char exec[1024];
        strncpy(exec, "/usr/local/bin/change_vagrantfile.py", 1024);
        fprintf(stdout, "executing %s\n", exec);
        system(exec);
    }
}

/* 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 env valiable */
    putenv("VAGRANT_HOME=/root/.vagrant.d/");
    putenv("VAGRANT_DEFAULT_PROVIDER=kvm");


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

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

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

 if ( strncmp(argv[1], "init", 4) == 0 ) {
     exec_init(uid, gid);
 } else if ( strncmp(argv[1], "destroy", 4) == 0 ) {
  if (execl(command, command, destroy_command, NULL) < 0) {
   perror("Execl:");
  }
 } else if ( strncmp(argv[1], "box", 3) == 0 ) {
    if ( strncmp(argv[2], "add", 3) == 0 ) {
        if (execl(command, command, box_add_command, NULL) < 0) {
            perror("Execl:");
        }
    } else if (strncmp(argv[2], "list", 4) == 0 ) {
        if (execl(command, command, box_command, box_list_command, NULL) < 0) {
            perror("Execl:");
        }
    }
 } else if ( strncmp(argv[1], "up", 2) == 0 ) {
  if (execl(command, command, up_command, provider_arg, NULL) < 0) {
   perror("Execl:");
  }
 } else if ( strncmp(argv[1], "ssh", 3) == 0 ) {
     if (execl(command, command, ssh_command, NULL) < 0) {
       perror("Execl:");
     }
 } else {
    usage();
    exit(1);
 }
 exit(0);
}

/* end */