11
|
1
|
|
2
|
|
3 #include <stdlib.h>
|
|
4 #include <unistd.h>
|
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <sys/types.h>
|
17
|
8 #include <sys/wait.h>
|
11
|
9 #include <unistd.h>
|
|
10 #include <signal.h>
|
|
11 #include <string.h>
|
|
12
|
|
13 #include <sys/types.h>
|
|
14 #include <regex.h>
|
|
15
|
15
|
16 #include <time.h>
|
11
|
17 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
|
|
18
|
|
19 #define command "/usr/bin/vagrant"
|
|
20 #define init_command "init"
|
|
21 #define up_command "up"
|
|
22 #define destroy_command "destroy"
|
|
23 #define ssh_command "ssh"
|
16
|
24 #define box_command "box"
|
|
25
|
|
26 #define box_add_command "add"
|
|
27 #define box_list_command "list"
|
17
|
28 #define box_default_url "http://ie.u-ryukyu.ac.jp/vagrant/fedora-19.box"
|
16
|
29
|
14
|
30 #define provider_arg "--provider=kvm"
|
11
|
31
|
12
|
32 #define NEW(type) ((type*)malloc(sizeof(type)))
|
|
33
|
15
|
34 #define VAGRANT_FILE "Vagrantfile"
|
|
35
|
11
|
36 /* Define global variables */
|
|
37
|
|
38 void
|
|
39 usage()
|
|
40 {
|
|
41 printf("Usage: COMMAND [init|up|destroy|ssh]\n");
|
|
42 }
|
|
43
|
15
|
44 void
|
|
45 exec_init(int uid, int gid)
|
|
46 {
|
|
47 pid_t pid = fork();
|
|
48 if (pid < 0) {
|
|
49 perror("fork");
|
|
50 exit(-1);
|
|
51 } else if (pid == 0) {
|
|
52 if (execl(command, command, init_command, NULL) < 0) {
|
|
53 perror("Execl:");
|
|
54 }
|
17
|
55 } else { // grant to edit vagrantfile to user
|
15
|
56 sleep(2);
|
|
57 if (chown(VAGRANT_FILE, uid, gid) != 0) {
|
|
58 printf("chown error.\n");
|
|
59 exit(1);
|
|
60 }
|
|
61 char exec[1024];
|
|
62 strncpy(exec, "/usr/local/bin/change_vagrantfile.py", 1024);
|
|
63 fprintf(stdout, "executing %s\n", exec);
|
|
64 system(exec);
|
|
65 }
|
|
66 }
|
|
67
|
17
|
68 void
|
|
69 exec_box_add(char *box_name)
|
|
70 {
|
|
71 pid_t pid = fork();
|
|
72 if (pid < 0) {
|
|
73 perror("fork");
|
|
74 exit(-1);
|
|
75 } else if (pid == 0) {
|
|
76 if (execl(command, command, box_command, box_add_command, box_name, box_default_url, NULL) < 0) {
|
|
77 perror("Execl:");
|
|
78 }
|
|
79 } else {
|
|
80 int status = 0;
|
|
81 printf("wait...\n");
|
|
82 if (wait(&status) == -1) {
|
|
83 perror("wait");
|
|
84 }
|
|
85 if (!WIFEXITED(status) == -1) {
|
|
86 perror("wait");
|
|
87 }
|
|
88 char exec[1024];
|
|
89 strncpy(exec, "/usr/local/bin/vagrant_newvm.py -n ", 1024);
|
|
90 strncat(exec, box_name, 1024);
|
|
91 fprintf(stdout, "executing %s\n", exec);
|
|
92 system(exec);
|
|
93 }
|
|
94 }
|
|
95
|
11
|
96 /* main(int argc, char **argv) - main process loop */
|
|
97
|
|
98 int main(int argc, char **argv)
|
|
99 {
|
|
100 int gid;
|
|
101 int uid;
|
|
102
|
|
103 /* Set euid and egid to actual user */
|
|
104
|
|
105 char *name = getlogin();
|
|
106 uid = getuid();
|
|
107 gid = getgid();
|
|
108 printf("uid %d gid %d name %s\n", uid,gid,name);
|
|
109 setegid(getgid());
|
|
110 seteuid(getuid());
|
|
111
|
|
112 regex_t *pattern = NEW(regex_t);
|
|
113 if (regcomp(pattern, name, 0) != 0) {
|
|
114 exit(0);
|
|
115 }
|
|
116
|
|
117 /* Confirm user is in GROUP(999) group */
|
|
118
|
|
119 /*
|
|
120 if ( gid != 999 ) {
|
|
121 printf("User Not Authorized! Exiting...\n");
|
|
122 exit(1);
|
|
123 }
|
|
124 */
|
15
|
125
|
|
126 /* Set env valiable */
|
14
|
127 putenv("VAGRANT_HOME=/root/.vagrant.d/");
|
|
128 putenv("VAGRANT_DEFAULT_PROVIDER=kvm");
|
|
129
|
11
|
130
|
|
131 /* Set uid, gid, euid and egid to root */
|
|
132
|
|
133 setegid(0);
|
|
134 seteuid(0);
|
|
135 setgid(0);
|
|
136 setuid(0);
|
|
137
|
|
138 /* Check argv for proper arguments and run
|
|
139 * the corresponding script, if invoked.
|
|
140 */
|
|
141
|
|
142 if ( strncmp(argv[1], "init", 4) == 0 ) {
|
15
|
143 exec_init(uid, gid);
|
11
|
144 } else if ( strncmp(argv[1], "destroy", 4) == 0 ) {
|
12
|
145 if (execl(command, command, destroy_command, NULL) < 0) {
|
11
|
146 perror("Execl:");
|
|
147 }
|
16
|
148 } else if ( strncmp(argv[1], "box", 3) == 0 ) {
|
|
149 if ( strncmp(argv[2], "add", 3) == 0 ) {
|
17
|
150 char box_name[1024] = "default_box";
|
|
151 strncpy(box_name, argv[3], 1024);
|
|
152 exec_box_add(box_name);
|
16
|
153 } else if (strncmp(argv[2], "list", 4) == 0 ) {
|
|
154 if (execl(command, command, box_command, box_list_command, NULL) < 0) {
|
|
155 perror("Execl:");
|
|
156 }
|
|
157 }
|
11
|
158 } else if ( strncmp(argv[1], "up", 2) == 0 ) {
|
14
|
159 if (execl(command, command, up_command, provider_arg, NULL) < 0) {
|
11
|
160 perror("Execl:");
|
|
161 }
|
14
|
162 } else if ( strncmp(argv[1], "ssh", 3) == 0 ) {
|
11
|
163 if (execl(command, command, ssh_command, NULL) < 0) {
|
|
164 perror("Execl:");
|
|
165 }
|
|
166 } else {
|
|
167 usage();
|
|
168 exit(1);
|
|
169 }
|
|
170 exit(0);
|
|
171 }
|
|
172
|
|
173 /* end */
|