view ie-vagrant.c @ 14:7003cdf42392

can up vagrant machine in user environment (not root)
author taiki
date Mon, 11 Nov 2013 20:44:34 -1000
parents 4e34b48bf7c7
children f194adc597b6
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>

/********************************************
 * 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 provider_arg "--provider=kvm"

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

/* Define global variables */

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

/* 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);
 }
 */
    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 ) {
  if (execl(command, command, init_command, NULL) < 0) {
   perror("Execl:");
  }
 } else if ( strncmp(argv[1], "destroy", 4) == 0 ) {
  if (execl(command, command, destroy_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 */