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